Exit Character Collection

I am trying to avoid a character set, so I get a set of variables, but I run into problems. Here's the MWE:

macro escape_all(x...) :($(esc.(x))...) end x = 1 y = 2 z = 3 macroexpand(:(@escape_all xyz)) 

It returns

 :(((:($(Expr(:escape, :x))), :($(Expr(:escape, :y))), :($(Expr(:escape, :z))))...,)) 

but what I'm looking to return is just

 (x,y,z) 
+5
source share
1 answer

Expr clearly works:

 julia> macro escape_all(xs...) Expr(:tuple, esc.(xs)...) end @escape_all (macro with 1 method) julia> @macroexpand @escape_all xyz :((x, y, z)) 

But you can also use the following unquote syntax in a context in which it seems to me to use list matching (e.g. ,@ in Lisp):

 julia> macro escape_list(xs...) :([$(esc.(xs)...)]) end @escape_list (macro with 1 method) julia> macro escape_f(xs...) :(f($(esc.(xs)...))) end @escape_f (macro with 1 method) julia> @macroexpand @escape_list xyz :([x, y, z]) julia> @macroexpand @escape_f xyz :((Main.f)(x, y, z)) 

Funny, I never saw $(x...) say anywhere. I stumbled upon this having recently read the code. But it is referred to in the current "recent" documents as splatting interpolation .

+3
source

Source: https://habr.com/ru/post/1275934/


All Articles