# Macro to convert list of variables to Dict keyed by variable name

**URL:** https://discourse.julialang.org/t/macro-to-convert-list-of-variables-to-dict-keyed-by-variable-name/8755
**Category:** General Usage
**Tags:** macros, metaprogramming
**Created:** [February 1, 2018, 9:23pm UTC](https://discourse.julialang.org/t/macro-to-convert-list-of-variables-to-dict-keyed-by-variable-name/8755 "2018-02-01T21:23:23Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![arghhhh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arghhhh/32/258_2.png) [@arghhhh](https://discourse.julialang.org/u/arghhhh)
#### Post date: [February 1, 2018, 9:23pm UTC](https://discourse.julialang.org/t/macro-to-convert-list-of-variables-to-dict-keyed-by-variable-name/8755/1 "2018-02-01T21:23:23Z")

</div>

For debugging purposes, I’d like to be able to write something like:

> d = @makedict( a, b, c )

which would expand to

> d = Dict( :a=\>a, :b=\>b, :c=\>c )

I just can’t seem to work out how to do this…

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [February 1, 2018, 9:24pm UTC](https://discourse.julialang.org/t/macro-to-convert-list-of-variables-to-dict-keyed-by-variable-name/8755/2 "2018-02-01T21:24:29Z")

</div>

See `@pack` in Parameters.jl

```julia
d = Dict{Symbol,Any}(:a=>5.0,:b=>2,:c=>"Hi!")
@unpack a, c = d
a == 5.0 #true
c == "Hi!" #true

d = Dict{Symbol,Any}()
@pack d = a, c
d # Dict{Symbol,Any}(:a=>5.0,:c=>"Hi!")

```

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [February 1, 2018, 10:55pm UTC](https://discourse.julialang.org/t/macro-to-convert-list-of-variables-to-dict-keyed-by-variable-name/8755/3 "2018-02-01T22:55:37Z")

</div>

```julia
julia> a = 1; b = 2; c = 3;

julia> macro makedict(vars...)
           expr = Expr(:call, :Dict)
           for i in 1:length(vars)
               push!(expr.args, :($(QuoteNode(vars[i])) => $(esc(vars[i]))))
           end
           return expr
       end
@makedict (macro with 1 method)

julia> d = @makedict(a, b, c)
Dict{Symbol,Int64} with 3 entries:
  :a => 1
  :b => 2
  :c => 3

```

---

<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: [February 1, 2018, 11:05pm UTC](https://discourse.julialang.org/t/macro-to-convert-list-of-variables-to-dict-keyed-by-variable-name/8755/4 "2018-02-01T23:05:16Z")

</div>

> [@mohamed82008](#):
>
> $(vars[i])

`esc`

> [@mohamed82008](#):
>
> $vars[$i]

`QuoteNode(vars[i])`

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [February 1, 2018, 11:14pm UTC](https://discourse.julialang.org/t/macro-to-convert-list-of-variables-to-dict-keyed-by-variable-name/8755/5 "2018-02-01T23:14:16Z")

</div>

Merci!
