Syntax error when interpolating a quoted operator into a quoted expression

I thought I understood how expression interpolation worked. This works as expected:

julia> sym = :a
:a

julia> :(b > $sym)
:(b > a)

But when I try to interpolate an operator into an expression, I get a weird syntax error:

julia> op = :-
:-

julia> :(1 $op 2)
ERROR: syntax: missing comma or ) in argument list

julia> op = :<
:<

julia> :(1 $op 2)
ERROR: syntax: missing comma or ) in argument list

Can anyone explain what’s going on here?

You can’t interpolate using infix notation. Do instead:

julia> op = :-
:-

julia> :($op(1,  2))
:(1 - 2)
1 Like

Thanks! Good to know.

Any idea why that’s not allowed?

Any idea why that’s not allowed?

I guess (without any deep understanding of what’s going on) interpolation happens after lowering.

So :(b > $sym) is first lowered to :(>(b, $sym)) and then interpolation happens. Whereas :(1 $op 2) is invalid syntax because it doesn’t recognize $op as an infix operator.

2 Likes