Scala Language String Interpolation Custom string interpolators


Example

It is possible to define custom string interpolators in addition to the built-in ones.

my"foo${bar}baz"

Is expanded by the compiler to:

new scala.StringContext("foo", "baz").my(bar)

scala.StringContext has no my method, therefore it can be provided by implicit conversion. A custom interpolator with the same behavior as the builtin s interpolator would then be implemented as follows:

implicit class MyInterpolator(sc: StringContext) {
  def my(subs: Any*): String = {
    val pit = sc.parts.iterator
    val sit = subs.iterator
    // Note parts.length == subs.length + 1
    val sb = new java.lang.StringBuilder(pit.next())
    while(sit.hasNext) {
      sb.append(sit.next().toString)
      sb.append(pit.next())          
    }
    sb.toString
  }
}

And the interpolation my"foo${bar}baz" would desugar to:

new MyInterpolation(new StringContext("foo", "baz")).my(bar)

Note that there is no restriction on the arguments or return type of the interpolation function. This leads us down a dark path where interpolation syntax can be used creatively to construct arbitrary objects, as illustrated in the following example:

case class Let(name: Char, value: Int)

implicit class LetInterpolator(sc: StringContext) {
  def let(value: Int): Let = Let(sc.parts(0).charAt(0), value)
}

let"a=${4}" // Let(a, 4)
let"b=${"foo"}" // error: type mismatch
let"c=" // error: not enough arguments for method let: (value: Int)Let