If I define a print function that only takes numbers as:
def print[T <% Number](value:T) {}
print: [T](value: T)(implicit evidence$1: (T) => java.lang.Number)Unit
I can call the above with:
print(5)
print(5.5)
print(6L)
But not with a String:
print("aaa")
<console>:7: error: could not find implicit value for evidence parameter of type (java.lang.String) => java.lang.Number
print("aaa")
This is expected.
But if I define the print function as:
def print2[T <% Number]: T => Unit = value => { }
print2: [T](implicit evidence$1: (T) => java.lang.Number)(T) => Unit
Notice how the implicit parameter is the first parameter instead of the last.
If I try to manually define the above function:
def print3[T](implicit f: (T) => java.lang.Number)(value:T):Unit = { }
<console>:1: error: '=' expected but '(' found.
def print3[T](implicit f: (T) => java.lang.Number)(value:T):Unit = { }
Basically the above is not a valid function definition but the compiler creates it when I previously defined print2.
When I call print2 with an Int:
print2(5)
<console>:7: error: type mismatch;
found : Int(5)
required: (?) => java.lang.Number
print2(5)
if I parameterize it:
print2[Int](5)
<console>:7: error: type mismatch;
found : Int(5)
required: (Int) => java.lang.Number
print2[Int](5)
It looks like it can't find the implicit conversion from scala.Int => java.lang.Integer.
How can I redefine print such that it returns functions and also accesses implicits in the correct way?
scalacimplementation. If you think about it, implicit parameters at any position other than the end makes no sense. Let's say this is alloweddef foo(implicit i: Int)(j: Float)(implicit: k: Int)(l: Double) = .... What exactly does it mean for the applicationfoo(1)(2)(3)? It's ambigious. - Y.H Wong