How does implicit types in scala work with reference to this https://youtu.be/hC4gGCD3vlY?t=263.
Also I did not understand why he mentions that the convertAtoB object is static.
How does implicit types in scala work with reference to this https://youtu.be/hC4gGCD3vlY?t=263.
Also I did not understand why he mentions that the convertAtoB object is static.
Let's start scala REPL with
implicitConversion
flag.Say you want to have all the integers wrapped inside Number string as
"Number(input)"
.Now instead of calling a function each time to convert int to desired type, you can define an implicit method which once sees your input and output as defined in your implicit convertor will do it for you.
example,
Note that
NumberToString
is a singleton class or whatstatic
class is in Java world.Now if you just define a variable of type
Int
, there will be no conversion happening because it is already of typeInt
and compiler is happy.But, if you give it a different type, then compiler will look for implicit methods, and picks up the the one which matches.