Basically I have a slew of optionals coming in as JSON, because as we all know even the most strict schema has edge case missing values. When trying to turn Option[A] to string with the standard interpolator it looks like:


So here's a better way that uses an interpolator for Option[A] which yields code that looks like:

val city = None  
val country = Some("us")  
val language = None  
val decibels = Some(14)




And here's the magic, mostly stolen from the Scala StringContext source code:

implicit class OptionStringInterpolator(val sc: StringContext) extends AnyVal {  

    def o(args: Any*) = optionInterpolator(StringContext.treatEscapes, args)

    private def optionInterpolator(process: String => String, args: Seq[Any]): String = {
      val pi = parts.iterator
      val ai = args.iterator
      val builder = new StringBuilder(process(
      while (ai.hasNext) {
        val resolved = match {
          case Some(s) => s
          case None => ""
          case o => o
        builder append resolved
        builder append process(

    def checkLengths(args: Seq[Any]): Unit =
      if (parts.length != args.length + 1)
        throw new IllegalArgumentException("wrong number of arguments ("+ args.length 
          +") for interpolated string with "+ parts.length +" parts")