# How does Julia merge several exprs to one expr?

**URL:** <https://discourse.julialang.org/t/how-does-julia-merge-several-exprs-to-one-expr/77498>\
**Category:** New to Julia\
**Created:** [March 7, 2022, 9:45am UTC](https://discourse.julialang.org/t/how-does-julia-merge-several-exprs-to-one-expr/77498 "2022-03-07T09:45:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [March 7, 2022, 9:45am UTC](https://discourse.julialang.org/t/how-does-julia-merge-several-exprs-to-one-expr/77498/1 "2022-03-07T09:45:38Z")

</div>

There is the problem:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/b/2b20bacbf9ceb8a4a6c9a5b9b3d84971de5eb1f0.png)

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [March 7, 2022, 10:25am UTC](https://discourse.julialang.org/t/how-does-julia-merge-several-exprs-to-one-expr/77498/3 "2022-03-07T10:25:05Z")

</div>

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

```julia
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

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [March 7, 2022, 11:26am UTC](https://discourse.julialang.org/t/how-does-julia-merge-several-exprs-to-one-expr/77498/4 "2022-03-07T11:26:29Z")

</div>

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:

```julia
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

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [March 7, 2022, 10:32pm UTC](https://discourse.julialang.org/t/how-does-julia-merge-several-exprs-to-one-expr/77498/6 "2022-03-07T22:32:12Z")

</div>

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

> [@Can I concatenate expressions in macro?](https://discourse.julialang.org/t/can-i-concatenate-expressions-in-macro/69220/2):
>
> You could look at how a block of expressions is normally represented and try to rebuild that. For example julia\> a = quote a = 1+1 b = a + 4 end quote #= REPL[16]:2 =# a = 1 + 1 #= REPL[16]:3 =# b = a + 4 end julia\> dump(a) Expr head: Symbol block args: Array{Any}((6,)) 1: LineNumberNode line: Int64 2 file: Symbol REPL[16] 2: Expr head: Symbol = args: Array{Any}((2,)) 1: Symbol a 2: Expr head: Sy…

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [March 7, 2022, 11:17pm UTC](https://discourse.julialang.org/t/how-does-julia-merge-several-exprs-to-one-expr/77498/7 "2022-03-07T23:17:30Z")

</div>

> [@Brian1](#):
>
> 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.

You might want to take a look at [`StaticModules.jl`](https://github.com/MasonProtter/StaticModules.jl); maybe it can cover some of what you need:

```julia
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

```

---

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [March 8, 2022, 3:48am UTC](https://discourse.julialang.org/t/how-does-julia-merge-several-exprs-to-one-expr/77498/8 "2022-03-08T03:48:04Z")

</div>

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