# Initialize multiple variables in one line

**URL:** <https://discourse.julialang.org/t/initialize-multiple-variables-in-one-line/28916>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 18, 2019, 7:41pm UTC](https://discourse.julialang.org/t/initialize-multiple-variables-in-one-line/28916 "2019-09-18T19:41:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 18, 2019, 7:41pm UTC](https://discourse.julialang.org/t/initialize-multiple-variables-in-one-line/28916/1 "2019-09-18T19:41:25Z")

</div>

Hello,  
I would like to initialize multiple variables in one line.  
The following code does not work:

```julia
a = b = c = Set{Symbol}()

```

If I do:

```julia
push!(a, :test)

```

all three variables get the same value.  
I just want to initialize all of them as empty set of Symbols, but later they shall contain different values.  
Any idea how to achieve this in one line?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [September 18, 2019, 8:24pm UTC](https://discourse.julialang.org/t/initialize-multiple-variables-in-one-line/28916/2 "2019-09-18T20:24:18Z")

</div>

```julia
a = Set{Symbol}(); b = Set{Symbol}(); c = Set{Symbol}()

```

---

<div class="post-metadata">

**Author:** ![dellison](https://avatars.discourse-cdn.com/v4/letter/d/ce73a5/32.png) [@dellison](https://discourse.julialang.org/u/dellison)\
**Post date:** [September 18, 2019, 8:25pm UTC](https://discourse.julialang.org/t/initialize-multiple-variables-in-one-line/28916/3 "2019-09-18T20:25:57Z")

</div>

This should work too:

```julia
a, b, c = Set{Symbol}(), Set{Symbol}(), Set{Symbol}()

```

---

<div class="post-metadata">

**Author:** ![zaki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zaki/32/10300_2.png) [@zaki](https://discourse.julialang.org/u/zaki)\
**Post date:** [September 18, 2019, 8:29pm UTC](https://discourse.julialang.org/t/initialize-multiple-variables-in-one-line/28916/4 "2019-09-18T20:29:24Z")

</div>

Perhaps this might work as well

```julia
a, b, c = repeat([Set{Symbol}()], 3)

```

Edit: Nope 😢

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 18, 2019, 8:30pm UTC](https://discourse.julialang.org/t/initialize-multiple-variables-in-one-line/28916/5 "2019-09-18T20:30:59Z")

</div>

Well, in reality I do not have to initialize three variables, but more. So a shorter notation would be nice to have.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 18, 2019, 8:31pm UTC](https://discourse.julialang.org/t/initialize-multiple-variables-in-one-line/28916/6 "2019-09-18T20:31:43Z")

</div>

This would be nice. Any idea for a solution that is as short as this one, but works?

---

<div class="post-metadata">

**Author:** ![zaki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zaki/32/10300_2.png) [@zaki](https://discourse.julialang.org/u/zaki)\
**Post date:** [September 18, 2019, 8:33pm UTC](https://discourse.julialang.org/t/initialize-multiple-variables-in-one-line/28916/7 "2019-09-18T20:33:44Z")

</div>

How about

```julia
a, b, c = [Set{Symbol}() for _ = 1:3]

```

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 18, 2019, 8:34pm UTC](https://discourse.julialang.org/t/initialize-multiple-variables-in-one-line/28916/8 "2019-09-18T20:34:32Z")

</div>

This doesn not work:

```julia
a,b,c =Tuple(repeat([Set{Symbol}()], 3))

```

Same problem, if I change on of the variables the others get changed too.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 18, 2019, 8:39pm UTC](https://discourse.julialang.org/t/initialize-multiple-variables-in-one-line/28916/9 "2019-09-18T20:39:01Z")

</div>

```julia
a, b, c = [Set{Symbol}() for _ = 1:3]

```

Looks good! 🙂

Thank you!

---

<div class="post-metadata">

**Author:** ![cchderrick](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@cchderrick](https://discourse.julialang.org/u/cchderrick)\
**Post date:** [September 18, 2019, 8:54pm UTC](https://discourse.julialang.org/t/initialize-multiple-variables-in-one-line/28916/10 "2019-09-18T20:54:44Z")

</div>

> [@zaki](#):
>
> a, b, c = repeat([Set{Symbol}()], 3)

Works for me on v1.0.3 and v1.2.0?

```julia
julia> a, b, c = repeat([Set{Symbol}()], 3)
3-element Array{Set{Symbol},1}:
 Set([])
 Set([])
 Set([])

julia> a
Set(Symbol[])

julia> b
Set(Symbol[])

julia> c
Set(Symbol[])

julia>   

```

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 18, 2019, 9:39pm UTC](https://discourse.julialang.org/t/initialize-multiple-variables-in-one-line/28916/11 "2019-09-18T21:39:32Z")

</div>

But if you change one variable, you change all. So not a valid solution.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 18, 2019, 9:48pm UTC](https://discourse.julialang.org/t/initialize-multiple-variables-in-one-line/28916/12 "2019-09-18T21:48:59Z")

</div>

> [@zaki](#):
>
> ```julia
> a, b, c = [Set{Symbol}() for _ = 1:3]
> 
> ```

Save an allocation by not using an array:

```julia
a, b, c = (Set{Symbol}() for _ = 1:3)

```

or

```julia
a, b, c = ntuple(_->Set{Symbol}(), 3)

```

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 19, 2019, 11:40am UTC](https://discourse.julialang.org/t/initialize-multiple-variables-in-one-line/28916/13 "2019-09-19T11:40:08Z")

</div>

Thank you for your advanced suggestions! 😃

---

<div class="post-metadata">

**Author:** ![traktofon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/traktofon/32/591_2.png) [@traktofon](https://discourse.julialang.org/u/traktofon)\
**Post date:** [September 19, 2019, 12:08pm UTC](https://discourse.julialang.org/t/initialize-multiple-variables-in-one-line/28916/14 "2019-09-19T12:08:19Z")

</div>

The proposed solutions still seem to violate DRY (a bit 😉) because the number of the variables on the left-hand side of the assignment must match the number on the right-hand side. Unfortunately I don’t see a way to fix this without a macro. Perhaps the following:

```julia
macro multidef(ex)
   @assert(ex isa Expr)
   @assert(ex.head == :(=))
   vars = ex.args[1].args
   what = ex.args[2]
   rex = quote end
   for var in vars
      push!(rex.args, :($(esc(var)) = $what))
   end
   rex
end

```

It can be used as follows:

```julia
@multidef a, b, c = Set{Symbol}()

```

Which has the right outcome:

```julia
julia> a,b,c
(Set(Symbol[]), Set(Symbol[]), Set(Symbol[]))

julia> a===b
false

```

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 19, 2019, 6:57pm UTC](https://discourse.julialang.org/t/initialize-multiple-variables-in-one-line/28916/15 "2019-09-19T18:57:47Z")

</div>

Thanks for this beautiful macro!  
But it is only useful if you really use it a lot, otherwise it has too much overhead.

---

<div class="post-metadata">

**Author:** ![Thibaut\_Lamadon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thibaut_lamadon/32/14309_2.png) [@Thibaut\_Lamadon](https://discourse.julialang.org/u/Thibaut_Lamadon)\
**Post date:** [January 9, 2021, 11:53pm UTC](https://discourse.julialang.org/t/initialize-multiple-variables-in-one-line/28916/16 "2021-01-09T23:53:55Z")

</div>

just curious, why didn’t you have the `esc` around `what`?

---

<div class="post-metadata">

**Author:** ![traktofon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/traktofon/32/591_2.png) [@traktofon](https://discourse.julialang.org/u/traktofon)\
**Post date:** [January 10, 2021, 11:24am UTC](https://discourse.julialang.org/t/initialize-multiple-variables-in-one-line/28916/17 "2021-01-10T11:24:22Z")

</div>

> [@Thibaut\_Lamadon](#):
>
> just curious, why didn’t you have the `esc` around `what` ?

You’re right, it probably makes sense to `esc` the `what` as well.

---

<div class="post-metadata">

**Author:** ![Thibaut\_Lamadon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thibaut_lamadon/32/14309_2.png) [@Thibaut\_Lamadon](https://discourse.julialang.org/u/Thibaut_Lamadon)\
**Post date:** [January 11, 2021, 4:24pm UTC](https://discourse.julialang.org/t/initialize-multiple-variables-in-one-line/28916/18 "2021-01-11T16:24:35Z")

</div>

Here it is for the record. I find it super useful, also to learn about writing macros, thanks for sharing!

```julia
macro multidef(ex)
    @assert(ex isa Expr)
    @assert(ex.head == :(=))
    vars = ex.args[1].args
    what = ex.args[2]
    rex = quote end
    for var in vars
       push!(rex.args, :( $(esc(var)) = $(esc(what)) ))
    end
    rex
end

```

---

<div class="post-metadata">

**Author:** ![ZhiyuanYao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhiyuanyao/32/15488_2.png) [@ZhiyuanYao](https://discourse.julialang.org/u/ZhiyuanYao)\
**Post date:** [May 2, 2021, 9:56am UTC](https://discourse.julialang.org/t/initialize-multiple-variables-in-one-line/28916/19 "2021-05-02T09:56:28Z")

</div>

What is the benefit of using the second approach, tuple comprehension? How allocation is saved?

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [May 2, 2021, 10:15am UTC](https://discourse.julialang.org/t/initialize-multiple-variables-in-one-line/28916/20 "2021-05-02T10:15:17Z")

</div>

```julia
julia> testarray() = [Set{Symbol}() for _ = 1:3]

julia> testtuple() = (Set{Symbol}() for _ = 1:3)

julia> (vector = (@allocated testarray()), tuple = (@allocated testtuple()))
(vector = 1552, tuple = 0)

```

Vectors are not of an unchangeable length, so we can add elements to them; they are stored indirectly. Tuples are fixed in size and other ways, so they are stored directly (immediately in very fast microprocessor-accessible memory).

[Next page](https://discourse.julialang.org/t/initialize-multiple-variables-in-one-line/28916.md?page=2)
