I make extensive use of the Pimp my Library pattern, and I'd like to remove the boilerplate. For example, say I have some trait PrettyPrint:
trait PrettyPrint { def prettyPrint: String }
If I want to pimp Int and Double, I need to write code like this:
implicit def int2PrettyPrint(self: Int) =
new PrettyPrint { def prettyPrint = "Int: " + self }
implicit def double2PrettyPrint(self: Double) =
new PrettyPrint { def prettyPrint = "Double: " + self }
In the above, I'd classify as boilerplate: 1) The name of the implicit conversion, 2) The "new" keyword, 3) Perhaps the argument name "self", 4) Perhaps the "implicit" keyword. I'd rather write something like this:
@pimp[Int, PrettyPrint] { def prettyPrint = "Int: " + self }
@pimp[Double, PrettyPrint] { def prettyPrint = "Double: " + self }
On the right hand sides of the above code, the name "self" is assumed to be the conversion argument.
Ideas on how to do this?
Some notes:
1) I'm amenable to using Scala 2.10 if necessary.
2) The new implicit classes in Scala 2.10 don't suffice as far as I can tell. This is because there is only one implicit conversion for each implicit class. In other words, code like the following wouldn't compile because PrettyPrint is declared twice:
implicit class PrettyPrint(self: Int) = ...
implicit class PrettyPrint(self: Double) = ...