Following is a solution which allows to write the implementation of primitiveShowInstance
only once, that's why it scales perfectly for more involved cases. The idea is pretty simple: we introduce another implicit, which only serves as an evidence of support for primitiveShowInstance
implementation by a certain type.
trait Show[ a ]{
def show( a: a ): String
}
trait PrimitiveShowInstance {
implicit def primitiveShowInstance[ a ]( implicit support: Support[ a ] ) =
new Show[ a ] {
def show( a: a ) = a.toString
}
// Simply an evidence of support for this by type `value`.
class Support[ value ]
}
object SomeModule extends PrimitiveShowInstance {
implicit val intPrimitiveShowInstanceSupport = new Support[Int]
implicit val stringPrimitiveShowInstanceSupport = new Support[String]
}