Macros with multiple parameters

The problem is that when you write:

@a @b x

the parser treats that as calling macro @b with argument x, then calling macro @a with just one argument.

You can see that by using dump to look at the syntax tree:

julia> dump(:(@a @b x))
Expr
  head: Symbol macrocall
  args: Array{Any}((3,))
    1: Symbol @a
    2: LineNumberNode
      line: Int64 1
      file: Symbol REPL[5]
    3: Expr
      head: Symbol macrocall
      args: Array{Any}((3,))
        1: Symbol @b
        2: LineNumberNode
          line: Int64 1
          file: Symbol REPL[5]
        3: Symbol x

or just by constructing the expression and letting julia add some parentheses to disambiguate:

julia> :(@a @b x)
:(#= REPL[6]:1 =# @a #= REPL[6]:1 =# @b(x))

And you can fix this by adding the parentheses that you actually want instead:

julia> @tm1(@__DIR__(), begin
         3 + 3
       end)
tm1 dir: /home/user
6

In this case, it’s also sufficient to just add parens on the call to @__DIR__ to avoid it trying to grab the whole begin block as an argument:

julia> @tm1 @__DIR__() begin
         3 + 3
       end
tm1 dir: /home/user
6
2 Likes