Implicit conversions can only work if the type being converted does not already have a method with a given signature. As everything has a toString
, it is not possible to override this by pimping.
What you might do is use a typeclass (akin to scalaz.Show
) which looks like this:
trait Show[-A] {
def show(a : A): String
}
Then you can use show
everywhere instead of toString
. Ideally what you want then is to make the Show[Any]
instance a very low priority implicit.
implicit val DateShow = new Show[Date] { def show(d : Date) = "whatever" }
trait LowPriorityShows {
implicit val AnyShow = new Show[Any] { def show(a : Any) = a.toString }
}
P.S. The reason I would not suggest using scalaz.Show
is that the return type is List[Char]
, which is just not practicable for most uses