Reputation: 7735
I have a simple function, that I use to generate generic Spec2 tests:
def check[T <: ED](actual: Seq[T], expectedFile: File)(implicit format: Format[T], ordering: Ordering[T]): Seq[MatchResult[Any]]
def generateCheck(clientName: String, patientId: String, folder: File)
Where generateCheck calls check allot. I want instead of defining check as a separate function, use it as functional argument for generateChecks
I want to have something like this:
def generateCheck(clientName: String, patientId: String, folder: File, check: ???)
For now I made a workaround, and just override check as needed, but I still wonder How can I define check as a functional argument?
Upvotes: 2
Views: 105
Reputation: 8462
Ok. I actually thought this is simple, but it is not. I am missing a bit of insights into your code, but next may give you some ideas:
I don't think you can do implicits with anonymous functions. At least I haven't seen that anywhere done. But you can easily wrap it:
class Check[T]()(implicit format: Format[T]) extends ((Seq[T], File) => Unit) {
override def apply(v1: Seq[T], v2: File): Unit = ??? // your implementation
}
So:
def test(check: Check[Int]): Unit = {
check(Seq.empty, new File("."))
}
test(new Check[Int]())
Or if you can refactor the function that doesn't need implicit context (I just think having implicit class is nice here, but not sure it is applicable to your case):
implicit class Check[T](val f: (Seq[T], File) => Unit)(implicit format: Format[T]) extends ((Seq[T], File) => Unit) {
override def apply(v1: Seq[T], v2: File): Unit = f(v1, v2) //do some processing and call f
}
Now:
def test(check: Check[Int]): Unit = {
check(Seq.empty, new File("."))
}
test((a: Seq[Int], b: File) => {
println("Hello world")
})
But you need to remember to put types into your function, or manually wrap it in new Check[T]
Upvotes: 1