Let’s start with Scala, as it coined the term Pimp My Library 10 years ago.

Scala’s approach is based on conversion. Consider a base type lacking the desired behavior. For example, Java’s double primitive type - mapped to Scala’s scala.Double type, is pretty limited.

The first step is to create a new type with said behavior. Therefore, Scala provides a RichDouble type to add some methods e.g. isWhole() .

The second step is to provide an implicit function that converts from the base type to the improved type. The signature of such a function must follow the following rules:

Have a single parameter of the base type

Return the improved type

Be tagged implicit

Here’s how the Scala library declares the Double to RichDouble conversion function:

private [ scala ] abstract class LowPriorityImplicits { ... implicit def doubleWrapper ( x : Double ) = new runtime . RichDouble ( x ) ... }

An alternative is to create an implicit class, which among other requirements must have a constructor with a single parameter of base type.

The final step step is to bring the conversion in scope. For conversion functions, it means importing the function in the class file where the conversion will be used. Note that in this particular case, the conversion function is part of the automatic imports (there’s no need to explicitly declare it).

At this point, if a function is not defined for a type, the compiler will look for an imported conversion function that transforms this type to a new type that provides this function. In that case, the type will be replaced with the conversion function.