We had an interesting case of enhancing the functionality of a logging library with our own case to feed data into DataDog. That brought us back to the quick discussion on implicits in Scala.
Implicits in a quick sense is Magic! Too much of it and you get lost. In the right balance it is quite useful. One of the favorite examples that I use is from our Scala Training classes where we are working with functional objects. When we are working with functional objects we want all operations to take functional objects as well.
Let us assume that we are a Vulcan and we operate with VulcanMoney. Does that get your Star Trek adrenaline going 😉
As you would notice, we have an add method which also takes a VulcanMoney. So essentially if we want to add some value to VulcanMoney, then we would have to add a VulcanMoney as well. Essentially, something like this would work
but what if we need something like this to work?
The compiler needs some help to make this happen. In come the implicits. We define an implicit method which would convert the integer to VulcanMoney.
and boom our code works now! The only restriction being that this method definition should be in scope. What if you have 2 methods of the same kind i.e. f:(Int)=>VulcanMoney ? Simple the compiler would complain. Isn’t Scala strongly typed with a powerful compiler 🙂
With Scala 2.10 we got another goody where we could extend a class with something called an implicit class. An implicit class is a class marked with the implicit keyword. This keyword makes the class’ primary constructor available for implicit conversions when the class is in scope.
Let us assume that we want to add a decorate method to the existing String class. How do we do it?
Simple, we define an implicit class called DecoratedString which makes the primary constructor available for conversion to String. Now, we can call the shiny new “decorate” method on any string. Hence something like this would work
Thus, either using implicit methods or classes we can easily extend the behavior with the help of the compiler.