Tuesday, June 19, 2012

Scala implicit: confusing


scala> implicit val x=23
x: Int = 23
scala> def dos(implicit d:Int)(s:Int) = println(d+s)
:1: error: '=' expected but '(' found.
       def dos(implicit d:Int)(s:Int) = println(d+s)
                              ^
scala> def dos(s:Int)(implicit d:Int) = println(d+s)
dos: (s: Int)(implicit d: Int)Unit


So the implicit argument can't be the first one on a curried function.






scala> def dos(d: Int, implicit s: Int) = println(d+s)
:1: error: identifier expected but 'implicit' found.
       def dos(d: Int, implicit s: Int) = println(d+s)
                       ^
Can't declare an argument as implicit if it is not the first one on a non-curried function.





scala> def dos(implicit d:Int, s: Int) = println(d+s)
dos: (implicit d: Int, implicit s: Int)Unit

First argument of a non-curried function can be implicit but the rest of the arguments automatically become implicit!

No comments: