Weird error - "macros application do not support named and/or default arguments"

233 views
Skip to first unread message

Alexandru Nedelcu

unread,
Jan 15, 2014, 4:14:02 AM1/15/14
to scala-user

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?

--
Alexandru Nedelcu
www.bionicspirit.com

PGP Public Key:
https://bionicspirit.com/key.aexpk

Eugene Burmako

unread,
Jan 16, 2014, 4:11:07 PM1/16/14
to scala...@googlegroups.com
Weird indeed. I'll try to take a look at this before RC1.

Eugene Burmako

unread,
Jan 22, 2014, 6:20:00 AM1/22/14
to scala...@googlegroups.com

Alexandru Nedelcu

unread,
Jan 22, 2014, 10:10:26 AM1/22/14
to Eugene Burmako, scala-user
Awesome, thanks Eugene, will this make it in Scala 2.11 ?


--
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.

Eugene Burmako

unread,
Jan 22, 2014, 10:12:43 AM1/22/14
to Alexandru Nedelcu, scala-user
Let's hope so. We still have almost a month until the release of 2.11.0-RC1.
Reply all
Reply to author
Forward
0 new messages