Implicit Conversions In Scala: Let’s extend Functionality

Reading Time: 3 minutes

Hello folks, in this blog we will see Implicit Conversions in Scala language. And how it helps to make our code more readable. Before going into much detail about implicit let’s understand why we need implicit in Scala. There is a basic difference between your own code and libraries of other people. You can change your code as your need but when you have to use someone else’s libraries you need to take them as they are and here implicit conversions and parameters come into action. They are also used in resolving type check errors.

Scala implicit allows you to omit calling method or parameter directly. For example, you can write a function that converts int to/from string explicitly but you can ask the compiler to do the same thing for you, implicitly.

An Implicit conversions in scala can be use in three ways-

1)Implicit Function

The implicit function allows you to provide extension methods or functions to pretty much any type or class. An implicit function called automatically when compiler thinks it’s good to do so. It means that if your code doesn’t compile but would if a call made to an implicit function, scala will call that function to make it compile.we will see this in more detail through a simple example. 

object ImplicitFunction extends App {
 val i = 1
 val intaddition = Seq(i).map { n => n + 1 }

 implicit def intToSeq(i: Int): Seq[Int] = Seq(i)
 println(i + 10)
 println( + 2))

In the above code snippet, I have defined one implicit function that converts Integer value to the sequence of an integer. Normally when you try to perform map operation on the variable of type Int it gives message error: value map is not a member of Int. So implicit function makes compiler works for you, for a  line println( + 2))  compiler checks list of implicit that are there any implicit that convert Int to the type that has map method and it found an implicit method “intToSeq” that converts Int to Seq[Int].In this way, we can convert Int to Seq[Int]  using an implicit function. 

2)Implicit Parameter:

An implicit parameter is a parameter to a function which annotated with an implicit keyword. Implicit parameter values automatically get passed by the compiler when not provided by the programmer. It means if no value supplied when called compiler looks for its implicit value in its implicit scope.

object ImplicitParam extends App{
 def multiply(implicit by: Int) = 5 * by

 implicit val multiplier=2

In the given code snippet, method multiply has an implicit parameter so when we called method multiply without providing parameter it looks for its implicit value. The compiler first looks for implicit definitions and implicit parameters that can be accessed directly at the point the method with implicit parameter block is called. Then it looks for members marked implicit in all the companion objects associated with the implicit type.

While when you define two implicit values of the same type in the same implicit scope of implicit parameter it gives an error as  ambiguous implicit values, because compiler gets confused between these two implicit values. If we did not define any implicit value in its scope it will give an error as “could not find implicit value for parameter”.

You can also define more than one implicit parameters for a method.

Following are the different way to declare implicit parameters.

def functionName(x:Int)(implicit y:Int)

def  functionName(implicit x:Int,y:Int,z:Int)

In the first line, only variable “y” is declared as implicit while in second syntax all three variables “x”,”y”,”z” are implicit. 

Now we will see some improper ways of declaring implicit parameters that give a compile-time error.

def functionName(implicit x:Int)(implicit y:Int)

def functionName(implicit x:Int)(y:Int)

3)Implicit Class:

The implicit class used to extend the behaviour of the existing class. Implicit classes are just like implicit functions in the way that they are resolved.

object ImplicitClass extends App {
 implicit class Bike(i: Int) {
   def start: Unit = println("bike is starting")
   def stop: Unit = println("bike is stop")
 val i: Int = 4

So in the above code snippet, the compiler checks whether Int has an inbuilt start or stop method? Nope? , then it will check is there an implicit in-scope that takes an Int and has a start and stop method then it found implicit class “Bike” with these scenarios. It’s meaning that it doesn’t matter if your type is constructor argument or method parameter. It just matters if there is  X that can convert it to a type Y that has a method Z.

This is how we implement the Implicit feature of scala in our program…

Thanks for reading! Keep learning!

References :