# Group of variables to named tuple: built-in solution?

**URL:** <https://discourse.julialang.org/t/group-of-variables-to-named-tuple-built-in-solution/16531>\
**Category:** General Usage\
**Created:** [October 19, 2018, 1:31pm UTC](https://discourse.julialang.org/t/group-of-variables-to-named-tuple-built-in-solution/16531 "2018-10-19T13:31:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jtackm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jtackm/32/4784_2.png) [@jtackm](https://discourse.julialang.org/u/jtackm)\
**Post date:** [October 19, 2018, 1:31pm UTC](https://discourse.julialang.org/t/group-of-variables-to-named-tuple-built-in-solution/16531/1 "2018-10-19T13:31:16Z")

</div>

I regularly have the use case that I would like to convert a group of variable references to a named tuple with the reference names as keys and the referenced values as vals (usually to improve self-documentation of code when returning results from a function call):

```julia
X = 1, Y = (1,2), Z = 0.9
t = (X = X, Y = Y, Z = Z)

-> (X = 1, Y = (1, 2), Z = 0.9)

```

To practice metaprogramming, I came up with the following macro to do this:

```julia
macro nt(ex)
    Expr(:tuple, [Expr(:(=), esc(arg), arg) for arg in ex.args]...)
end

@nt X, Y, Z

-> (X = 1, Y = (1, 2), Z = 0.9)

```

However, in order to avoid copying this macro everywhere, is anyone aware of a built-in way in julia to do this?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 19, 2018, 1:40pm UTC](https://discourse.julialang.org/t/group-of-variables-to-named-tuple-built-in-solution/16531/2 "2018-10-19T13:40:36Z")

</div>

I am not aware of a built-in solution, which is why I wrote

> **[GitHub - tpapp/EponymTuples.jl: Julia package for deconstructing dispatch on...](https://github.com/tpapp/EponymTuples.jl/)**
>
> Julia package for deconstructing dispatch on NamedTuples. - GitHub - tpapp/EponymTuples.jl: Julia package for deconstructing dispatch on NamedTuples.

---

<div class="post-metadata">

**Author:** ![jtackm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jtackm/32/4784_2.png) [@jtackm](https://discourse.julialang.org/u/jtackm)\
**Post date:** [October 19, 2018, 1:51pm UTC](https://discourse.julialang.org/t/group-of-variables-to-named-tuple-built-in-solution/16531/3 "2018-10-19T13:51:25Z")

</div>

Thanks, looks like a nicely focused and lightweight dependency. I will definitely use that if no pure-`Base` solution is available.

---

<div class="post-metadata">

**Author:** ![chriscoey](https://avatars.discourse-cdn.com/v4/letter/c/3ab097/32.png) [@chriscoey](https://discourse.julialang.org/u/chriscoey)\
**Post date:** [October 19, 2018, 5:09pm UTC](https://discourse.julialang.org/t/group-of-variables-to-named-tuple-built-in-solution/16531/4 "2018-10-19T17:09:46Z")

</div>

Would be nice to see that functionality in Base!

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [October 19, 2018, 6:28pm UTC](https://discourse.julialang.org/t/group-of-variables-to-named-tuple-built-in-solution/16531/5 "2018-10-19T18:28:54Z")

</div>

If [https://github.com/JuliaLang/julia/issues/29333](https://github.com/JuliaLang/julia/issues/29333) happens then it would be natural for `(; X, Y, Z)` to be shorthand for the named tuple `(X=X, Y=Y, Z=Z)`.

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [October 19, 2018, 6:56pm UTC](https://discourse.julialang.org/t/group-of-variables-to-named-tuple-built-in-solution/16531/6 "2018-10-19T18:56:18Z")

</div>

> [@jtackm](#):
>
> usually to improve self-documentation of code when returning results from a function call

I had to stare a while at your question to understand what you were getting at.  
Please let me know if the following example captures the essence of your scenario.

```julia
julia> stats(data) = mean(data), extrema(data), std(data)
stats (generic function with 1 method)

julia> stats(3:41)
(22.0, (3, 41), 11.40175425099138)

```

What do those numbers mean?

So you rewrite the function to make it more self documenting

```julia
julia> stats(data) = begin
          avg=mean(data)
          min_max=extrema(data)
          std_dev=std(data)
          return (avg=avg, min_max=min_max, std_dev=std_dev)
       end
stats (generic function with 1 method)

julia> stats(3:41)
(avg = 22.0, min_max = (3, 41), std_dev = 11.40175425099138)

```

But you don’t like the return statement `(avg=avg, min_max=min_max, std_dev=std_dev)`  
Instead you’d prefer to write

```julia
return @nt avg, min_max, std_dev

```

or with Stefan’s syntax

```julia
return (; avg, min_max, std_dev)

```

Do I understand your scenario correctly?

---

<div class="post-metadata">

**Author:** ![jtackm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jtackm/32/4784_2.png) [@jtackm](https://discourse.julialang.org/u/jtackm)\
**Post date:** [October 19, 2018, 8:27pm UTC](https://discourse.julialang.org/t/group-of-variables-to-named-tuple-built-in-solution/16531/7 "2018-10-19T20:27:55Z")

</div>

> [@StefanKarpinski](#):
>
> If [https://github.com/JuliaLang/julia/issues/29333](https://github.com/JuliaLang/julia/issues/29333) happens then it would be natural for `(; X, Y, Z)` to be shorthand for the named tuple `(X=X, Y=Y, Z=Z)` .

I’m not completely sure yet if I like the general proposal in that issue (variable names translating directly to keyword argument keys), but this syntax would indeed be concise for the named tuple construction case. While I’m not sure I would have guessed this syntax correctly, it may also become more intuitive after one is used to thinking in the “variable name translates directly to keyword” mindset proposed in the issue.

> [@jandehaan](#):
>
> Do I understand your scenario correctly?

Yes, that is a nice summary of the use-case.

---

<div class="post-metadata">

**Author:** ![samoconnor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samoconnor/32/1802_2.png) [@samoconnor](https://discourse.julialang.org/u/samoconnor)\
**Post date:** [October 19, 2018, 10:59pm UTC](https://discourse.julialang.org/t/group-of-variables-to-named-tuple-built-in-solution/16531/8 "2018-10-19T22:59:29Z")

</div>

[GitHub - JuliaCloud/SymDict.jl: Dictionaries with Symbol keys.](https://github.com/samoconnor/SymDict.jl) has a macro for capturing local variables into a Dict (this is ~4 years old, so it was pre-named-tuples).

```julia
a = 1
b = 2
@SymDict(a,b)

Dict{Symbol,Any}(:a=>1,:b=>2)

```

(AWSCore.jl uses `Dict{Symbol}` to pass around various config parameters, options, request meta-data etc. I have been thinking recently that it might be better to use named tuples for this, or even just `Vector{Pair{Symbol}}`)
