Scala implicit class

Slowly getting into the more advanced features of Scala.

Starting with implicit class.

implicit class allows you to add methods to a type.

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 the AnyVal form all the time ? There are a multitude of reasons, I could go over each and every ones of them, but this Stackoverflow answer explains it in a lot of details.

Reveal more information and clues
Load Exercise