Scala implicit class

Slowly getting into the more advanced features of Scala.

Starting withimplicit class.

implicit classallows you to add methods to atype.

The syntax is like this:

implicit class NAME(value_name: TypeToOverride) {

def methodToAdd: OutputType = ???

}

You might have noticed an other form:

implicit class NAME(val value_name: TypeToOverride) extends AnyVal {

def methodToAdd: OutputType = ???

}

The second form will be more efficient, it will not create a new instance. The first form will create a new instance of the Type which can be inefficient if overused. Then, why not use theAnyValform all the time ? There are a multitude of reasons, I could go over each and every ones of them, but thisStackoverflow answerexplains it in a lot of details.

Reveal more information and clues
Load Exercise