I’m toying around with macros and I’ve built this really simple assert() macro:
def assert(condition: Boolean): Unit =
macro assert_impl
def assert_impl(c: Context)(condition: c.Expr[Boolean]): c.Expr[Unit] = {
import c.universe._
val conditionRep = show(condition.tree)
val conditionRepTree = Literal(Constant(conditionRep))
val conditionRepExpr = c.Expr[String](conditionRepTree)
reify {
if (!condition.splice)
throw new AssertionError(conditionRepExpr.splice)
}
}
The problem is, when I try to apply this without a parameter, I get the error message mentioned in the title:
scala> assert()
<console>:11: error: macros application do not support named and/or default arguments
assert()
^
Now this was unexpected. I was under the impression that the application of a macro follows the rules of a plain function call. As in, I would expect the compiler to throw something like this:
scala> def assert(condition: Boolean): Unit = ()
scala> assert()
<console>:15: error: not enough arguments for method assert: (condition: Boolean)Unit.
Unspecified value parameter condition.
assert()
^
So how can one cope with this? What if you want optional parameters? And what to do for friendlier error messages?
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.