4

For me, I would use an implicit class under the following scenarios:

  • don't have access to the underlying type to be able to add the method I want.
  • the method I want doesn't make sense in a "global" sense.
  • i am splitting the functionality into another library of "extensions"
  • actually converting to a new type adds semantic/readability value (the new type actually means something)

However, I am fairly new to Scala (<6 months) and I'm noticing the developers around me are using implicit classes when it breaks the scenarios above. When I asked why, the answer was "because that's what I've always done".

So my question is, is there an official recommendation for when one should use an implicit class over a normal function added to the class definition? (I couldn't find anything here: https://docs.scala-lang.org/overviews/core/implicit-classes.html)

1
  • 2
    I would say that as it is with any advanced tool, you should not use unless it makes life much better in this particular case. Commented Jan 18, 2018 at 14:01

2 Answers 2

2

As per the SIP,

Motivation for the implicit class was that the popular extension method pattern, sometimes called the Pimp My Library pattern was used in Scala to extend pre-existing classes with new methods, fields, and interfaces. There was also another common ‘extension’ use case known as type traits or type classes (see scala.math.Numeric). Type classes offered an alternative to pure inheritance hierarchies that was very similar to the extension method pattern. The main drawback to both of these techniques was that they suffered the creation of an extra object at every invocation to gain the convenient syntax. This made these useful patterns unsuitable for use in performance-critical code. In these situations it was common to remove use of the pattern and resort to using an object with static helper methods.

And implicit class syntax was thus added to solve these issues.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi @Anurag, when you say " In these situations it was common to remove use of the pattern and resort to using an object with static helper methods. And implicit class syntax was thus added to solve these issues" can you please give examples? I thought you were saying implict class for both use cases create performance issues, but then the last sentence seems saying implict class was added to solve these issues. another question: the Type class pattern, what if the implicit insntaces are added in the library companion object already, does it have performance issue?
Thank you @Anurag. now I also understand why sometime people call implicit class as implicit conversion, as underneath it has a implicit conversion apply method... but I am still confused about this sentence. And implicit class syntax was thus added to solve these issues. based on the link, it seems to me, if the compiler decides not to optimize it, the object will be still created. Please see Possible Implementation section. so implicit class itself, to my understanding, is not solvinng the extra object creation issue, maybe it gives developer or compiler opportunities to solve it
0

The rock. They allow to make your own DSLs. Take a look to the Spray code, one of our classic and beloved projects:

trait TransformerPipelineSupport { ... implicit class WithTransformation[A](value: A) { def ~>[B](f: A ⇒ B): B = f(value) } ... } 

The ~> allows to compose Spray directives... There are many more examples

1 Comment

Such automagic implicit conversion must not be used carelessly, and is generally evil

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.