Scala macros: local extension?

Can scala 2.11 macros force the macros to expand their parameters?

Here is my use case: I started with the printf macro from the documentation , and then made my own macro to concatenate strings.

def mconcat(s1: String, s2: String, s3: String): String = macro mconcat_impl def mconcat_impl(c: Context)(s1: c.Expr[String], s2: c.Expr[String], s3: c.Expr[String]): c.Expr[String] = { import c.universe._ c.Expr[String](q"""$s1.concat($s2.concat($s3))""") } 

I was hoping to combine these two macros,

 mprintf(mconcat("what", "a", "burger")) 

but got a matching error when expanding the macro.

EDIT

Thanks to Travis Brown for pointing out that mconcat does not extend to a string literal. Sorry about that! But the problem remains if we simplify the mconcat value for:

 c.Expr[String](q"""$s1""") 

or

 s1 

or even

 c.Expr[String](Literal(Constant("what"))) 

All three give the same error message:

 Test.scala:8: error: exception during macro expansion: scala.MatchError: ("what": String) (of class scala.reflect.internal.Trees$Typed) at Printf$.printf_impl(Printf.scala:23) mprintf(mconcat("what", "a", "burger")) ^ one error found 
+5
source share

Source: https://habr.com/ru/post/1234944/


All Articles