This works:
julia> :( x + y )
:(x + y)
I'd like to interpolate an operator instead of having a hard-coded +
. I can do this:
julia> op = :+
:+
julia> :( $op(x, y) )
:(x + y)
Is there a way to keep the x op y
syntax? This fails:
:( x $op y )
ERROR: syntax: missing comma or ) in argument list
I doubt it. If it's not syntactically okay it's hard to get further. Of course you can do string interpolation and Meta.parse
but you probably don't want that.
Thanks! Yes, I thought of Meta.parse
, but basically I was just wondering if I was missing something. The manual doesn't provide clarity about this.
I'm not sure why it fails, though. My mental model (which is likely wrong) is that :( x $op y )
is first converted to :( x + y )
; that is, interpolation happens before further parsing of the quote. Probably parsing occurs before interpolation.
It would be kinda hard to interpolate before parsing. E.g. making sense of
julia> op = [:+, :-]
2-element Vector{Symbol}:
:+
:-
julia> :($(op[2])(x, y))
:(x - y)
Last updated: Oct 02 2023 at 04:34 UTC