February 2018
Intermediate to advanced
552 pages
13h 46m
English
Implicit conversions are used to convert an element from a type A to another type B, when the element is used in a place where B is expected. Let's check it with the following example:
scala> val num = 10
num: Int = 10
scala> val numInString = "20"
numInString: String = 20
scala> val result = num * numInString
<console>:14: error: overloaded method value * with alternatives:
(x: Double)Double <and>
(x: Float)Float <and>
(x: Long)Long <and>
(x: Int)Int <and>
(x: Char)Int <and>
(x: Short)Int <and>
(x: Byte)Int
cannot be applied to (String)
val result = num * numInString
^
scala> implicit def str2Int(str:String) = str.toInt
str2Int: (str: String)Int
scala> val result = num * numInString
result: Int = 200
Explore our previously ...
Read now
Unlock full access