In simple terms, assume you have a method square root which accepts one argument and returns the square root of that number, then if you declare the argument as implicit, then you need to just have one implicit variable in main class and call the square root method without passing the argument.
Example:
object ImplicitExample {
def main(args: Array[String]): Unit = {
implicit val num = 4;
// Note: We are not passing argument explicitly. Compiler automatically find implicit variable in the class and pass that variable as argument.
println(sqrt); // same as ==> sqrt(num)
}
def sqrt(implicit number: Int) = {
Math.sqrt(number)
}
}
Output:
2.0
We can also have one implicit method (implicit def) and one method accepting one implicit variable, then in that case also, the compiler will pass that method output as parameter to that method call.
Example:
object ImplicitExample {
def sqrt(implicit number: Int) = {
Math.sqrt(number)
}
implicit def getNumber = 9
def main(args: Array[String]): Unit = {
// implicit val num = 4;
// Note: We are not passing argument explicitly. Compiler automatically find implicit variable or implicit function in the class and pass that variable or function as argument.
// The compiler would try to resolve this as sqrt(getNumber).
println(sqrt); // same as ==> sqrt(getNumber) ==> sqrt(9);
}
}
Output:
3.0
Implicit functions will be called automatically if the compiler thinks it’s a good idea to do so. What that means is that if your code doesn’t compile but would, if a call was made to an implicit function, Scala will call that function to make it compile.
However, you can’t have more than one in scope. So if we have both the num variable and getNumber function defined as implicit and call sqrt method, we’d get the following error.
So in short, It means if you have one implicit variable and methods which accept one implicit argument than if no value is supplied during method called, the compiler will look for an implicit value and pass it in for you.
So in above code, if you have more than one implicit variable, then you will get compile time error as per below image:
If you remove implicit keyword from argument, then it is mandatory to pass the argument to square root method while calling it.
If you don’t specify any implicit variable, then again it is mandatory to pass the argument to sqaure root method while calling it.
Syntax:
You can only use implicit once in a parameter list and all parameters following it will be implicit. For example;
Advantages:
Implicit parameters are useful for removing boiler plate parameter passing and can make your code more readable. So if you find yourself passing the same value several times in quick succession, they can help hide the duplication.
The Scala library often use them to define default implementations that are “just available”. When you come to need a custom implementation, you can pass one in explicitly or use your own implicit value. Example:
object ImplicitExample {
def sqrt(implicit number: Int) = {
Math.sqrt(number)
}
// implicit def getNumber = 9
def main(args: Array[String]): Unit = {
implicit val num = 4;
println(sqrt);
println(sqrt(9));
println(sqrt(16));
}
}
Output:
2.0
3.0
4.0
Creating Implicit Function and its real time use:
Scala implicit functions allow you to easily add extension methods or functions to any type or class.
Example: We will extend the String class such that it will have an isFavoriteDonut() function.
Steps:
1. How to create a wrapper String class which will extend the String type
Let us create a simple wrapper class called DonutString which will take the String type as its parameter and then provide an isFavoriteDonut() function.
class DonutString(s: String) {
def isFavoriteDonut: Boolean = s == "Glazed Donut"
}
2. How to create an implicit function to convert a String to the wrapper String class
It is a mandatory to encapsulate your implicit functions or conversions into a singleton using object. You cannot write your implict functions in class.
As such, let us define an implicitfunction named stringToDonutString which will take the Stringtype as its parameter and wire it through a new instance of the wrapperString class named DonutString from Step 1.
object DonutConverstions {
implicit def stringToDonutString(s: String) = new DonutString(s)
}
3. How to import the String conversion so that it is in scope
In order to use the implicit String function which will convert a String type into a DonutString type, you will have to have the implicit function from Step 2 in scope. This can be achieved using the import keyword as shown below:
import DonutConverstions._
NOTE: As part of the import expression, we are using the wildcard operator _ which will import any values or implicit functions.
4. How to access the custom String function called isFavaoriteDonut
import DonutConverstions._
object ImplicitExample {
def main(args: Array[String]): Unit = {
val glazedDonut = "Glazed Donut"
val vanillaDonut = "Vanilla Donut"
println(s"Is Glazed Donut my favorite Donut = ${glazedDonut.isFavoriteDonut}")
println(s"Is Vanilla Donut my favorite Donut = ${vanillaDonut.isFavoriteDonut}")
}
}
Output:
Is Glazed Donut my favorite Donut = true
Is Vanilla Donut my favorite Donut = false
Complete Program:
import DonutConverstions._
class DonutString(s: String) {
def isFavoriteDonut: Boolean = s == "Glazed Donut"
}
object DonutConverstions {
implicit def stringToDonutString(s: String) = new DonutString(s)
}
object ImplicitExample {
def main(args: Array[String]): Unit = {
val glazedDonut = "Glazed Donut"
val vanillaDonut = "Vanilla Donut"
println(s"Is Glazed Donut my favorite Donut = ${glazedDonut.isFavoriteDonut}")
println(s"Is Vanilla Donut my favorite Donut = ${vanillaDonut.isFavoriteDonut}")
}
}
Output:
Is Glazed Donut my favorite Donut = true
Is Vanilla Donut my favorite Donut = false
NOTE:
- The custom isFavoriteDonut() function looks built-into the String class.
- However, we did not have to manually modify the source code of the String class.
- Instead, we’ve used the secret powers of Scala’s implicit function to extend the String class.






