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
scala scala-macros


source to share


No one has answered this question yet

See similar questions:

4
how to print name and value of variable using scala macro?

or similar:

272
How to use Shapeless in Quasiquote?
8
Is the DAO pattern deprecated in Scala?
4
Scala ambiguity with seamless function calls
3
java.lang.NoClassDefFoundError: Failed to initialize class when starting spark job via spark-submit in scala code
3
Could not find Traverse for sequencing Seq [ValidationNel [String, MyCaseClass]] => ValidationNel [String, Seq [MyCaseClass]]
3
Scala execution string interpretation / formatting
2
Error: Could not find implicit value for parameter timeout: akka.util.Timeout
1
In a scala macro called with an implicit def, how can I get the original statement of the caller into the implicit def as a string?
1
Scala implicit Paramater used in Shapeless library
0
How to ensure that the implicit variable is passed from property to method?



All Articles
Loading...
X
Show
Funny
Dev
Pics