How to extend "s" string interpolator

I would like to create a new string interpolator macro, let's call it "X", that works like "s" when it comes to interpolating new values. The string that I pass to my interpolator is first passed through "s" and then the resulting string is processed with an "X" after processing. Optimally, I would like the line to be resolved before it even gets into the macro. Is this possible at compile time? I don't want the macro to know about any reference variables.

Thank!

implicit class XInterp (val sc : StringContext) {
   def X(args: Any*): String = macro XMacro
}

def XMacro(c: Context)(args: c.Expr[Any]*): c.Expr[String] = {
   // args(0).tree == "Foo has a value of 5 and this is awesome!"?
}

val foo = 5
val bar = "this is awesome!"
val result = X"Foo has a value of $foo and $bar"
println(result)

"Foo has a value of 5 and this is awesome!"

      

+3


source to share





All Articles