Scala Language String Interpolation Custom string interpolators


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


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 =
    val sit = subs.iterator
    // Note parts.length == subs.length + 1
    val sb = new java.lang.StringBuilder(
    while(sit.hasNext) {

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(, 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