Fun with Scala Implicits
by Peter.
Last night I discovered that Scala’s RichString has a format method quite similar to Python’s. However, in Python I’m used to using the % operator and I wondered if I could use the same syntax in Scala. With a little library pimping and help from my friends on Stack Overflow, I can:
class BFString(s: String) { def %(in: Any*) = s.format(in: _*) } implicit def add_%(s: String) = new BFString(s) |
scala> "The %s costs $%d." % ("dog", 255) res2: String = The dog costs $255.
Pretty cool!