Introduction to Scala Implicit
Implicit stands for we do not need to create or call some of the code in our program rather than the code being managed by the compiler itself. We do not need to call some functions explicitly. In some cases, it is governed by the compiler. In other words, we can say that this scale implicit allows us to ignore the reference of variables and sometimes call to a method. We rely on the compiler to do this task for us and make connections if any are required. Compilers do so many things for us without making so many changes to 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 at by the compiler if no value is 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.
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 rules 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 implicit variables by mentioning their data type as well. But remember, we are defining the keyword only at the start of making both variables 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 variables, but we want to make one of them non-implicit, so here, variable ‘a’ is non-implicit. We define that into a separate bracket. For ‘b’, we want to make it implicit, so we 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 declaration mentioned above is also not correct in defining 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 a common mistake while the declaration of implicit functions.
2. Implicit Functions
Scala implicit function can be defined by using implicit keywords 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 removing the boilerplate from the code. that means we can define the name of the implicit function. Then only one can use that name without declaring full type. Unnecessary code removal is also an advantage of implicit functions.
3. Implicit Classes
If you want to define an implicit class, 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 the removal of boilerplate, and also, we do not need to call methods and reference variable. The compiler can manage this, or we can say depends on the compiler to provide their values at the time of calling them.
Recommended Articles
We hope that this EDUCBA information on “Scala Implicit” was beneficial to you. You can view EDUCBA’s recommended articles for more information.