July 2017
Intermediate to advanced
796 pages
18h 55m
English
Implicit is another exciting and powerful feature introduced by Scala, and it can refer to two different things:
Actual automatic conversion can be accomplished with implicit def, as seen in the following example (supposing you are using the Scala REPL):
scala> implicit def stringToInt(s: String) = s.toIntstringToInt: (s: String)Int
Now, having the preceding code in my scope, it's possible for me to do something like this:
scala> def add(x:Int, y:Int) = x + yadd: (x: Int, y: Int)Intscala> add(1, "2")res5: Int = 3scala>
Even if one of the parameters passed to add() is a String ...
Read now
Unlock full access