How does Julia merge several exprs to one expr?

There is the problem:
image

Not sure I completely understand what you’re trying to achieve, but this might be one way to do it:

julia> nn=[:(at=at), :(ct=ct)]
2-element Vector{Expr}:
 :(at = at)
 :(ct = ct)

julia> :($(nn...),)
:((at = at, ct = ct))

But may I ask what the broader context is? Why do you need to build such expressions?


PS: Welcome to the Julia discourse! Please post quoted code, instead of screenshots; this makes it easier for us to help you

4 Likes

Thanks a lot to your help and your advice.
In broader context, my target is to creat a set of local varialbes. Firstly, create a NamedTuple came to my mind, but in practise, that is not convinient. So now I can get the target just like this:

macro localvars(expr)
    bb=[]
    for cc in expr.args
        if cc isa Expr && cc.head==:(=)
            append!(bb,[cc.args[1]])
        end
    end
    cvv=Expr(:tuple,(:($x=$x) for x in bb)...)
    bbres=quote
        let
            $expr
            $cvv
        end
    end |> esc
end

vars=@localvars begin
    a=1
    b=2
    c=a+b
end 

vars.a
vars.c

This is a little convinient for me

Pretty much the same as the answer, but maybe it can add something

You might want to take a look at StaticModules.jl; maybe it can cover some of what you need:

julia> using StaticModules

julia> @staticmodule vars begin
           a=1
           b=2
           c=a+b
       end
StaticModule vars containing
  a = 1
  b = 2
  c = 3

julia> vars.c
3

I read the doc of StaticModules.jl this morning, this pakage is exactly what I was handling on.
Thank you so much.