What is Scala Implicit?
As we have already stated that a Scala Implicit is simply a keyword that is used in this programming language, therefore, now we will try to dig deeper into the functionality of this keyword. In the Scala programming language, we can create implicit parameters by using the Implicit keyword. An implicit parameter is one that can take implicit values according to the context when no other value is explicitly assigned to them. Whenever we do not assign any value to a variable in Scala, the compiler always looks for the implicit value and automatically assigns it to that variable.
Features of Scala Implicit
Some of the most important features of the Scala Implicit are as follows:
- The Implicit value is only used if an explicit value is not passed to a function.
- The Implicit keyword can only be used once within a specific method.
How to Use the Scala Implicit in Ubuntu 20.04?
To learn more about the usage of the Scala Implicit in the simplest possible ways, you need to check out the following two examples:
Example 1: Multiplication of Two Numbers Using the Scala Implicit
In this example, we want to design a program that multiplies two numbers together. However, we want to perform this multiplication by keeping the multiplier implicit. To understand this strategy, you will have to visualize the Scala code shown below:
In this simple Scala script, we have created a class named “Implicits” with the “object” keyword and have defined our “main()” function within it. After that, we have created a value with the name “multiplicand” and have assigned to it an integer i.e., 12 in this case. Then, we have defined another value named “multiplier” while keeping it implicit with the use of the “implicit” keyword, and then, we have assigned to it another integer i.e., 2 in this case. Here, we would also like to state that a multiplicand is a number that is supposed to be multiplied whereas a multiplier is a number by which we wish to multiply the multiplicand.
Then, we have defined a function named “multiply”. This function is capable of accepting an implicit integer. We have named it “by”. We have equalized this function to the result of the statement “multiplicand * by”. It means that if we have created an implicit variable in this script, (which we have already done) then while performing the multiplication operation, the “by” variable will be replaced by the value of that implicit variable. It implies that in our particular case, 12 will be multiplied by 2. After that, we have assigned the result of the “multiply” function to a value named “output”. Finally, we have printed this output on the terminal using the “println” statement.
The Scala script that we have just created above can easily be compiled with the following command:
Then, its class file can be executed with the command shown below:
The result of the Scala script that we have created in this example is shown in the following image. It verifies that the output of our “multiply” function i.e., 24 is absolutely correct.
Example 2: Printing Messages on the Terminal with and without the Scala Implicit
Now, when you have learned how the Scala Implicit can be used within a Scala script, the next step is to draw a comparison between the output of a program with and without the Scala Implicit. To do that, we have implemented the Scala script shown below:
In our second example, we have used the same Scala class that we had created in our first example. However, within our “main()” function, this time, we have defined a value named “greeting” and have assigned to it a string “Hello ”. After that, we have defined another value “name” while keeping it implicit and have assigned to it a string “John”.
Then, we have defined a function named “display” which is capable of accepting an implicit string named “message”. We have equalized this function to the statement “greeting + message”. It means that this function will display the combined values stored in the “greeting” variable and the implicit “message” variable. After that, we have defined a value named “output1” and have assigned to it the result of the “display” function. Then, we have created another value named “output2” and have also assigned to it the result of the “display” function, however, this time, we have called this function using a different string i.e., Catherine. Because of this, the implicit value will not be used for calculating the “output2” rather the newly passed value will be used. Finally, we have used two “println” statements for printing both of these outputs on the terminal.
After compiling and executing this Scala program, the following output was displayed on the terminal. It shows that the first time when we did not pass any value to the “display” function, then the implicit value was used. On the other hand, when we passed a different value to the “display” function, then the implicit value was not used rather the passed value appeared as the output.
With the help of this article, we wanted to introduce you to the concept of the Scala Implicit in Ubuntu 20.04. For that, we discussed what Scala Implicit actually is and also shared some of its main features with you. Then, we explained two very basic examples to you that made use of the Scala Implicit in Ubuntu 20.04. After reading this article, you will quickly grasp this important concept of the Scala programming language.