I recently discovered that one can use the Pimp Enrich My Library pattern to add methods to companion objects using .type:
object Whatever { }
implicit class WhateverExtensions(val obj: Whatever.type) {
def greet = println("Hi!")
}
Whatever.greet
Unfortunately the same doesn't seem to work for package objects like scala.math:
implicit class MathExtensions(val obj: scala.math.type) {
def min(x: Money, y: Money): Money = ???
}
I get the following compiler error:
Error:(153, 47) type mismatch;
found : math.type
required: AnyRef
Note that math extends Any, not AnyRef.
Such types can participate in value classes, but instances
cannot appear in singleton types or in reference comparisons.
implicit class MathExtensions(val obj: scala.math.type) extends AnyVal {
^
Is it possible to enrich a package object?