Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type of a function with Implicit parameters in Scala

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.

like image 276
Mortimer Avatar asked Sep 03 '13 22:09

Mortimer


People also ask

What is implicit function in Scala?

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.

What is the type of a function in Scala?

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.

What are type parameters in Scala?

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.

What are implicit classes in Scala?

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.

How do you pass an implicit parameter?

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.

How many implicit parameters can an instance method have?

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.


1 Answers

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.

like image 182
Travis Brown Avatar answered Oct 21 '22 19:10

Travis Brown