Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between conversion with implicit function and implicit class in Scala

For implicit conversion in Scala, I can use either implicit conversion function

implicit def intToX(i:Int):X = new X(i)

1.myMethod // -1

or implicit class

implicit class X(i: Int) {
    def myMethod = - i
}

1.myMethod // -1

Is there any difference between the two? When should I prefer one over the other?

There is a related question about implicit conversion vs. type class but it only compares implicit functions and type classes. What I am interested is the difference from implicit classes.

like image 510
Mifeet Avatar asked Aug 29 '15 13:08

Mifeet


People also ask

What is an implicit conversion function in Scala?

Implicit conversions in Scala are the set of methods that are apply when an object of wrong type is used. It allows the compiler to automatically convert of one type to another. Implicit conversions are applied in two conditions: First, if an expression of type A and S does not match to the expected expression type B.

What is an implicit conversion?

An implicit conversion sequence is the sequence of conversions required to convert an argument in a function call to the type of the corresponding parameter in a function declaration. The compiler tries to determine an implicit conversion sequence for each argument.

What is a function called by Scala compiler when a type conversion is required?

1)Implicit Function: An implicit function called automatically when compiler thinks it's good to do so. It means that if your code doesn't compile but would if a call made to an implicit function, scala will call that function to make it compile.

What is implicit object in Scala?

The implicit system in Scala allows the compiler to adjust code using a well-defined lookup mechanism. A programmer in Scala can leave out information that the compiler will attempt to infer at compile time. The Scala compiler can infer one of two situations: A method call or constructor with a missing parameter.


2 Answers

Implicit class is a syntax sugar for implicit method and a class:

http://docs.scala-lang.org/sips/completed/implicit-classes.html:

For example, a definition of the form:

implicit class RichInt(n: Int) extends Ordered[Int] {
  def min(m: Int): Int = if (n <= m) n else m
  ...
}

will be transformed by the compiler as follows:

class RichInt(n: Int) extends Ordered[Int] {
  def min(m: Int): Int = if (n <= m) n else m
  ...
}

implicit final def RichInt(n: Int): RichInt = new RichInt(n)

Implicit classes were added in scala 2.10 because it is was very common to define new class with defining implicit method conversion to it.

But if you do not need to define a new class but defining implicit conversion to existing class you should better use an implicit method

like image 150

It's been a while sice you asked a question and it seems that things have changed since. Actually, you should always choose implicit classes over implicit conversions now.

Implicit conversions have been marked as an advanced language feature by scala and the compiler discourages use of them (at least in Scala 2.12). And you need to add an extra flag (-language:implicitConversions) to the compile options to turn it off. See scala-lang docs

Also, Scala community (or at least LightBend/Typesafe people) are even planning of getting rid of imlicit conversions in general. This was mention in a conference talk in 2017 Nov presenting Scala 2.13, you can find it here.

like image 36
eddyP23 Avatar answered Oct 28 '22 02:10

eddyP23