如何扩展" s"字符串插值器

时间:2015-05-11 06:48:04

标签: scala scala-macros

我想创建一个新的字符串插值器宏,让我们称之为' X',其功能类似于" s"当涉及到内插新值时。我传入内插器的字符串首先通过" s"然后由' X'对结果字符串进行后处理。最理想的是我喜欢在它击中宏之前要解析的字符串。这可能在编译时吗?我不希望宏知道任何引用的变量。

谢谢!

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!"

0 个答案:

没有答案