Implicits In Scala
I’ve been developing in Scala for close to a year now and it has been a jolly ride so far. One of the topics that I found tough to understand was implicits. This post gives a brief introduction to implicits with some sample code.
Implicit Functions — Implicit functions take a single parameter and are automatically called (if in scope or is defined in the companion object of the target type) on the source type variable if there is a type mis-match. For example, consider the following example —
In this example, I’ve defined an implicit function that takes in an integer and returns a string. This enables me to use all functions of String (target type) on Integers (source type). Note that, the Scala compiler wouldn’t convert all Integers to Strings and would convert them only if the code wouldn’t compile in the first place.
Implicit Parameters — Consider the example where you want to define a function which needs to take in a value and needs to pretty print it with the currency symbol. Instead of passing the currency symbol every time, you can define a function which takes in an implicit variable of type Currency. When the function printAmount() is called, an implicit variable of type Currency is needed for it to compile successfully.
Further Reading and Source Code — I’ve just touched the surface of implicits in Scala and there’s a lot more to it. Scala for the Impatient is a good book which would provide more information on this topic.
Code. Learn. Explore