📜 ⬆️ ⬇️

Implicit parameters and transformations in Scala

Having run through the previous articles on Habré, the tyts and the tyts were not able to quickly understand what makes implicit in Scala. Let's try to figure it out together.


So, implicit in Scala allows you to avoid calling methods or explicit references to variables, and instead allow the compiler to find the necessary implicit data itself.

For example, we could write a function to convert from Float to Int (FloatToInt) and, instead of calling this function explicitly, the compiler would do it instead of us implicitly:
')
def double(value: Int) = value * 2 implicit def FloatToInt(value: Float):Int = value.toInt println(double(2.5F)) 

Confused? Let's get everything in order.

So today we will look at two implicit categories, namely:


Implicit parameter


An implicit parameter is a function parameter preceded by the implicit keyword. It means that if no value is passed when the function is called, the compiler will look for the implicit parameter in its own hand and pass it for us.

For example, such a function that takes an implicit value parameter as input and doubles it:

 def double(implicit value: Int) = value * 2 

Without an implicit parameter, it will fall with an error:

 def double(implicit value: Int) = value * 2 println(double) // error: could not find implicit value for parameter value 

The explicitly passed parameter will work:

 def double(implicit value: Int) = value * 2 println(double(3)) // 6 

With the implicit parameter, it will look like this:

 def double(implicit value: Int) = value * 2 implicit val multiplier = 2 println(double) // 4 

But you need to be careful:

 def double(implicit value: Int) = value * 2 implicit val multiplier = 2 implicit val multiplier2 = 1 println(double) // error: ambiguous implicit values 

And finally, an example of passing as an implicit def parameter:

 def double(implicit value: Int) = value * 2 val cappucinoLarge: Boolean = false implicit def cappucinoPrice: Int = if (cappucinoLarge) 200 else 100 println(double) // 200 

Those. in the end we will succeed
 double(cappucinoPrice()) 

Syntax notes:

 def func1(implicit value1: Int) // value1  def func2(implicit value1: Int, value2: Int) // value1  value2  def func3(value1: Int, implicit value2: Int) //   def func4(value1: Int)(implicit value2: Int) //  value2  def func5(implicit value1: Int)(value2: Int) //   def func6(implicit value1: Int)(implicit value2: Int) //   

Implicit conversion


Returning to the Float example in Int:

 def double(value: Int) = value * 2 implicit def FloatToInt(value: Float):Int = value.toInt println(double(2.5F)) 

When we call double, we have a mismatch of the type of the expected parameter (Int) with the incoming parameter (Float). Therefore, the compiler is looking for any method in scope that is marked implicit and with the expected parameter (Int) and input parameter (Float) that are needed in this situation. Finds FloatToInt, performs the conversion, and passes on the desired value to double.

Now, I hope, it has become clearer. All success in the development of Scala!

Source: https://habr.com/ru/post/451358/


All Articles