Introduction to Scala Implicit
Implicit stands for we do not need to create or call some of the code in our program rather than code is managed by the compiler itself. We do not need to call some function explicitly in some cases it is managed by the compiler. In other words, we can say that this scala implicit allows us to ignore reference of variable and sometimes call to a method also and we rely on the compiler to do this task for us and make connections if any required. There are so many things that compiler do for us without making so many changes into the code.
Syntax
To work with scala implicit we have a different syntax which can be seen below but for all, we are using an implicit keyword to make any variable implicit.
1. def name(implicit a : data_type)
def demo1(implicit a: Int)
2. def name(implicit a : data_type, b : data_type)
def demo2(implicit a : Int, b : String)
3. def name(variabl_name: data_type)(implicit variabl_name : data_type)
def demo3(a: String)(implicit b : Int)
4. def name(implicit variabl_name: data_type)( variabl_name : data_type)
def demo3( implicit a: String)( b : Int) // but this way is not correct will generatecompile time error.
5. def name(implicit variabl_name: data_type)(implicit variabl_name : data_type)
def name(implicit a: Int)(implicit b : Int) //not correct just to show the different snta avilable
How Implicit functions work in Scala?
In scala implicit means the same as other languages. We can avoid some code to write it explicitly and this job is done by the compiler. In Scala, we have types of implicit i.e. implicit classes. Implicit parameters, implicit functions. Let’s discuss them one by one;
1. Implicit Parameters
Implicit parameters simply mean that if we are making any parameters implicit by using implicit keyword then it simply means that the variable value will be looked by the compiler if no value provided to it. Then the compiler will pass a value to it for us. We can use val, def, var with the implicit keyword to define our variable.
4.5 (5,543 ratings)
View Course
Example #1 – implicit def variable_name : Data_type
>> implicit def impvar : Int
Example #2 – implicit var variable_name : Data_type
>> implicit var impvar : Stiring
Example #3 – implicit val variable_name : Data_type
>> implicit val impvar : Float
Some rule for defining variables using the implicit keyword.
1. def name(implicit a : data_type)
def demo1(implicit a: Int)
Explanation: In this syntax above we will provide one name after def and inside it we can make our variable implicit by using implicit keyword followed by the data type of the variable.
2. def name(implicit a : data_type, b : data_type)
def demo2(implicit a : Int, b : String)
Explanation: In the above syntax we are defining two variables as an implicit variable by mentioning their data type as well. But remember we are defining the keyword only at the start of making both variable implicit.
3. def name(variabl_name: data_type)(implicit variabl_name : data_type)
def demo3(a: String)(implicit b : Int)
Explanation: In this syntax, we are defining two variable but we want to make one of them as non-implicit, so here variable ‘a’ is non-implicit we define that into a separate bracket. For ‘b’ this we want to make implicit so defined it into a separate bracket.
4. def name(implicit variabl_name: data_type)( variabl_name : data_type)
def demo3( implicit a: String)( b : Int)
Explanation: In this case, it will not work because this is not the right way to define it in scala.
5. def name(implicit variabl_name: data_type)(implicit variabl_name : data_type)
def demo4(implicit a: Int)(implicit b : Int)
Explanation: The above-mentioned declaration is also not correct to define implicit functions.
6. def name(implicit a : data_type, implicit b : data_type)
def demo5(implicit a : data_type, implicit b : data_type)
Explanation: In the above declaration it is the common mistake while the declaration of implicit functions.
2. Implicit Functions
Scala implicit function can we be defined by using implicit keyword see syntax below;
implicit Z => Y
This can also be defined as >> scala.ImplicitFunction[Z, Y]
The main advantage of using the implicit function is that they remove the boilerplate from the code. that means we can define the name of the implicit function. Then only can use that name without declaring full type. Unnecessary code removal is also an advantage od implicit functions.
3. Implicit Classes
If you want to define an implicit class then just use an implicit keyword before the class keyword.
implicit class class_name {
//code logic
}
In this way, we can define the implicit class in Scala.
Examples to Implement Scala Implicit
Below are the examples mentioned:
Example #1
In this example, we are defining and printing variable values.
Code:
object Main extends App{
// Your code here!
// declaring implicit variable
implicit def impval : Int = 20
implicit var impval1 : Int = 30
implicit val impval2 : String = "Hello i am implicit variable."
// printing their values.
println("Implicit variable value is :: " + impval)
println("Implicit variable value is :: " + impval1)
println("Implicit variable value is :: " + impval2)
}
Output:
Example #2
Calling a function with implicit parameters declared.
Code:
object Main extends App{
// Your code here!
// declaring implicit variable
implicit def impval : Int = 20
implicit var impval1 : Int = 30
implicit val impval2 : String = "Hello i am implicit variable."
// printing their values.
println("Implicit variable value is :: " + impval)
println("Implicit variable value is :: " + impval1)
println("Implicit variable value is :: " + impval2)
demo1(20, 50)
def demo1(implicit a: Int , b : Int){
println("value of a :: " + a)
println("value of b :: " + b)
var result = a + b
println("value after addition is :: " + result)
}
}
Output:
Conclusion
Scala Implicit provide us various advantage like removal of boilerplate and also we do not need to call methods and reference variable this can be managed by the compiler or we can say depends on the compiler to provide their values at the time of calling them.
Recommended Articles
This is a guide to Scala Implicit. Here we discuss an introduction to Scala Implicit, syntax, how does it work, and examples for understanding better. You can also go through our other related articles to learn more –