Reputation: 11244
The following works:
object X extends (Int => String => Long) {
def apply(x:Int):String => Long = ???
}
How could I type an apply
function with an implicit
parameter?
I have the following method:
def apply(x:Int)(implicit y:String):Long = ???
How can I describe the function type?
object X extends <functionType> {
def apply(x:Int)(implicit y:String):Long = ???
}
update
I could define it like this:
object X extends (Int => String => Long) {
def apply(x:Int):(String => Long) = ???
def apply(x:Int)(implicit y:String):Long = ???;
}
But then calling it doesn't work:
error: ambiguous reference to overloaded definition,
both method apply in object X of type (x: Int)(implicit y: String)Long
and method apply in object X of type (x: Int)String => Long
match argument types (Int)
X(3)
^
Upvotes: 4
Views: 332
Reputation: 10764
The only thing that comes to my mind is this:
object X {
def apply(i: Int)(implicit y: String): Long = ???
implicit def xToFun(x: X.type)(implicit y: String): Int => Long = x(_)
}
The implicit conversion will allow you to use X
wherever Int => Long
is needed, and the implicit String
parameter will be resolved when that conversion is applied (not when X.apply
is actually called):
val fun: Int => Long = X //error, no implicit String in scope
implicit val s = "fuu"
val fun: Int => Long = X //OK
Upvotes: 3