I would like to have a higher order function that takes in parameter a function that accepts a specific implicit parameter.
To be more precise, I am trying to make a function that takes a Future
creation method that depends on an implicit context and returns a method that doesn't depend on the context.
To be more concrete, let's say that I have something like this:
def foo(a: Int)(implicit ctx: ExecutionContext): Future[Float] = future { somelongBar... }
I would like to do have a method like this:
def provideCtx[A](func: ExecutionContext => A): A = {
val ctx = setupCtx
func(ctx)
}
but if I call provideCtx(foo)
, the compiler complains about the implicit execution context missing.
The fact that I am dealing with an ExecutionContext is not very important. What I would like to find is how to write the parameter type to accept a function with an implicit argument of a specific type. I understand that the implicit part is a curryed argument, so that in fact I have a function like so: ExecutionContext => Int => Future[Float]
, and I am pretty sure that at runtime, the jvm doesn't know that that ExecutionContext is implicit, but I can't make the compiler understand that.
Implicit parameters are the parameters that are passed to a function with implicit keyword in Scala, which means the values will be taken from the context in which they are called.
Scala functions are first-class values. Scala functions are first-class values. You must mention the return type of parameters while defining the function and the return type of a function is optional. If you don't specify the return type of a function, the default return type is Unit.
Methods in Scala can be parameterized by type as well as by value. The syntax is similar to that of generic classes. Type parameters are enclosed in square brackets, while value parameters are enclosed in parentheses.
Scala 2.10 introduced a new feature called implicit classes. An implicit class is a class marked with the implicit keyword. This keyword makes the class's primary constructor available for implicit conversions when the class is in scope. Implicit classes were proposed in SIP-13.
The implicit parameter in Java is the object that the method belongs to. It's passed by specifying the reference or variable of the object before the name of the method. An implicit parameter is opposite to an explicit parameter, which is passed when specifying the parameter in the parenthesis of a method call.
An implicit parameter list (implicit $p_1$,$\ldots$,$p_n$) of a method marks the parameters $p_1 , \ldots , p_n$ as implicit. A method or constructor can have only one implicit parameter list, and it must be the last parameter list given.
The problem is that foo
is a method, not a function, and eta-expansion (which converts methods to functions) is not attempted until after implicit application. See section 6.26.2 of the language specification for the details, and this issue for additional discussion.
One workaround would be to write something like this:
provideCtx((ctx: ExecutionContext) => (a: Int) => foo(a)(ctx))
I'm not sure a more generic solution is possible (at least without some kind of reflection, etc.), since we can't even refer to foo
(except in a method call, of course) without an implicit in scope.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With