# Bulding a NamedTuple / Dict / struct from variables without repetitions in the code

**URL:** https://discourse.julialang.org/t/bulding-a-namedtuple-dict-struct-from-variables-without-repetitions-in-the-code/14006
**Category:** New to Julia
**Created:** [August 24, 2018, 4:41pm UTC](https://discourse.julialang.org/t/bulding-a-namedtuple-dict-struct-from-variables-without-repetitions-in-the-code/14006 "2018-08-24T16:41:36Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![dylanfesta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dylanfesta/32/5125_2.png) [@dylanfesta](https://discourse.julialang.org/u/dylanfesta)
#### Post date: [August 24, 2018, 4:41pm UTC](https://discourse.julialang.org/t/bulding-a-namedtuple-dict-struct-from-variables-without-repetitions-in-the-code/14006/1 "2018-08-24T16:41:36Z")

</div>

Very often, especially when prototyping, I compute different quantities, and I want all of them to be easily accessible. The easiest way is to return a named tuple:

```julia
function foo()
    x = 1
    y = "hello" 
    z = [x,2,3]
    # ... and more 
    (x=x,y=y,z=z)
end 

```

Is there some way or an existing macro that I can use to avoid repetitions in the return argument “x=x” , “y=y” , etc ? Usually the variable names are much longer, and this is prone to errors.

In relation to that, I also have this issue when saving variables on a file, for example:

```julia
using JLD2
x=123
y = 456
jldopen("somefile.jld2","w") do f 
    f["x"]=x
    write(f,"y",y)
end

```

I have to write “x” and “y” twice. With the @save macro I would not have this issue, but it’s often the case that I do not want to save the whole working environment.

Thanks for your help!

---

<div class="post-metadata">

### Author: ![jmert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmert/32/3161_2.png) [@jmert](https://discourse.julialang.org/u/jmert)
#### Post date: [August 24, 2018, 4:50pm UTC](https://discourse.julialang.org/t/bulding-a-namedtuple-dict-struct-from-variables-without-repetitions-in-the-code/14006/2 "2018-08-24T16:50:48Z")

</div>

Thrown together really quickly:

```julia
macro returnnt(x...)
    ex = [:($(esc(z)) = $(esc(z))) for z in x]
    return :(return ($(ex...),))
end

function foo()
    x = 1
    y = "hello" 
    z = [x,2,3]
    # ... and more 
    @returnnt x y z
end 

```

I’m sure there yet be dragons lurking in the corners of this implementation, but as a proof of concept, hope it’s enough for you for a start.

---

<div class="post-metadata">

### Author: ![dylanfesta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dylanfesta/32/5125_2.png) [@dylanfesta](https://discourse.julialang.org/u/dylanfesta)
#### Post date: [August 24, 2018, 5:31pm UTC](https://discourse.julialang.org/t/bulding-a-namedtuple-dict-struct-from-variables-without-repetitions-in-the-code/14006/3 "2018-08-24T17:31:46Z")

</div>

Thaks a lot , it seems to do the job. I tried to define a macro, but I got lost in the elements that I needed to escape. I am probably asking too much now, but it would be cool to have something like this, as well…

```julia

my_values = @wishfulthinkingmacro begin 
    x = 1
    y = "hello" 
    z = [x,2,3]
end

using Testing
@test my_values.x == 1 # etc

```

zero repetitions and even less prone to typos ! I would try to build it myself, but of course I am very open to any advice.

---

<div class="post-metadata">

### Author: ![jmert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmert/32/3161_2.png) [@jmert](https://discourse.julialang.org/u/jmert)
#### Post date: [August 24, 2018, 6:23pm UTC](https://discourse.julialang.org/t/bulding-a-namedtuple-dict-struct-from-variables-without-repetitions-in-the-code/14006/4 "2018-08-24T18:23:36Z")

</div>

Well that was a fun diversion. **Way more caveats on this one (!!)**, but it works for the simple case you provided:

```julia
julia> using MacroTools

julia> using MacroTools: combinedef, postwalk

julia> macro autoreturn(f)
    fparts = splitdef(f)
    fbody = block(fparts[:body])
    vars = Symbol[]
    postwalk(fbody) do ex
        if @capture(ex, return x_)
            @warn "Explicit return seen!"
        end
        if @capture(ex, x_Symbol = y_)
            push!(vars, x)
        end
        ex
    end
    retpairs = [:($z = $z) for z in vars]
    push!(fbody.args, :(return ($(retpairs...),)))
    fparts[:body] = esc(fbody)
    return combinedef(fparts)
end

julia> @autoreturn function foo()
    x = 1
    y = "hello"
    z = [x,2,3]
end

julia> foo()
(x = 1, y = "hello", z = [1, 2, 3])

```

_Edit: minor clean-ups_

---

<div class="post-metadata">

### Author: ![dylanfesta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dylanfesta/32/5125_2.png) [@dylanfesta](https://discourse.julialang.org/u/dylanfesta)
#### Post date: [August 24, 2018, 11:50pm UTC](https://discourse.julialang.org/t/bulding-a-namedtuple-dict-struct-from-variables-without-repetitions-in-the-code/14006/5 "2018-08-24T23:50:09Z")

</div>

Thanks a lot for your help and very quick response ! It seems to work well for testing and prototyping… and hopefully I will gain some useful insights about metaprogramming.
