Can scala 2.11 macros force their parameters' macros to expand?
Here's 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 hoped to combine these two macros,
mprintf(mconcat("what", "a", "burger"))
but got a match error in macro expansion.
EDIT
Thanks to Travis Brown for pointing out that mconcat
doesn't expand to a string literal. Sorry about that! But there's still a problem if we simplify the value of mconcat
to:
c.Expr[String](q"""$s1""")
or to
s1
or even to
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