In Java, I would do something like
class MyDate extends java.util.Date {
public String toString() { ... }
}
MyDate date = new MyDate
A little bit clunky. In Scala, is it possible to override toString whilst still using regular java.util.Date instead of MyDate. I have an inkling implicits are involved but would be happy to use any technique
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:Then you can use
showeverywhere instead oftoString. Ideally what you want then is to make theShow[Any]instance a very low priority implicit.P.S. The reason I would not suggest using
scalaz.Showis that the return type isList[Char], which is just not practicable for most uses