# Symbol to variable in a macro

**URL:** <https://discourse.julialang.org/t/symbol-to-variable-in-a-macro/48924>\
**Category:** General Usage\
**Tags:** metaprogramming\
**Created:** [October 24, 2020, 9:13am UTC](https://discourse.julialang.org/t/symbol-to-variable-in-a-macro/48924 "2020-10-24T09:13:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [October 24, 2020, 9:13am UTC](https://discourse.julialang.org/t/symbol-to-variable-in-a-macro/48924/1 "2020-10-24T09:13:40Z")

</div>

An easy question. I have asked it some time back and got a good answer, but cannot find back to it, not on Discourse, not in the manual, and not in my code…

I want to write a macro to generate code like

```julia
getfield(vari,:something) = something

```

so I code

```julia
macro mack(out,name)
    return esc(quote
            getfield($out,$name) = $name
    end)
end
@mack(vari,:something)

```

and the rub is, on the right hand side of the assignment I want $name to be interpolated as `something`, not `:something`.

And yes, I reckon you can workaround _this example_ with

```julia
macro mack(out,name)
    return esc(quote
            $out.$name = $name
    end)
end
@mack(vari,something)

```

but that won’t help me for my real problem. 😀

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 24, 2020, 9:26am UTC](https://discourse.julialang.org/t/symbol-to-variable-in-a-macro/48924/2 "2020-10-24T09:26:39Z")

</div>

> [@Philippe\_Maincon1](#):
>
> ```julia
> getfield(vari,:something) = something
> 
> ```

This code doesn’t make sense so it is hard to know what you are trying to do. Maybe this is useful though (note the usage of `QuoteNode`):

```julia
julia> macro mack(name)
           return :($(QuoteNode(name)), $(esc(name)))
       end;

julia> @macroexpand @mack foo
:((:foo, foo))

julia> foo = 3; @mack foo
(:foo, 3)

```

---

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [October 24, 2020, 1:11pm UTC](https://discourse.julialang.org/t/symbol-to-variable-in-a-macro/48924/3 "2020-10-24T13:11:51Z")

</div>

Hi Kristoffer,

Guilty as charged. I did not test my simplification, and made it a nonsense. I wish I had written

```julia
macro mack(out,name)
    return esc(quote
            setfield!($out,$name,$name)
    end)
end
@mack(vari,:something)

```

which (modified with help), would return

```julia
setfield!(vari,:something,something) # vari.something = something

```

I take note of the QuoteNode, which does the trick if the macro call is

```julia
@mack vari something

```

however, I want to write a macro called like this

```julia
@mack vari :something

```

(Well, of course, the conclusion may be that giving a Symbol as macro argument is just a way to make life difficult…) But assuming I stick to the later call syntax, any way to interpolate `something` (not `:something`) in the code?

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [October 24, 2020, 1:20pm UTC](https://discourse.julialang.org/t/symbol-to-variable-in-a-macro/48924/4 "2020-10-24T13:20:55Z")

</div>

Whenever you write macros, you should invest time into running `dump` on any syntax you plan to process:

```julia
julia> dump(:(x))
Symbol x

julia> dump(:(:x))
QuoteNode
  value: Symbol x

```

If it’s important that users pass in something that syntactically looks like a symbol, you need to do the relevant unwrapping or wrapping inside your macro to handle that case.

---

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [October 24, 2020, 1:30pm UTC](https://discourse.julialang.org/t/symbol-to-variable-in-a-macro/48924/5 "2020-10-24T13:30:45Z")

</div>

First: dump is a very nice function I didn’t know about! Good tip.

Second: what you say is, if the user call the macro with `:something`, having examined (with dump) the Expression that the macro receives, I can go grab subexpression directly: the function I was looking for, I can write myself.

---

<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 24, 2020, 1:35pm UTC](https://discourse.julialang.org/t/symbol-to-variable-in-a-macro/48924/6 "2020-10-24T13:35:26Z")

</div>

> [@Philippe\_Maincon1](#):
>
> I want to write a macro to generate code like
> 
> ```julia
> getfield(vari,:something) = something
> 
> ```

Are you sure you want to solve this by macros? If you provide some context, perhaps you will get a solution that does not need metaprogramming at all.

---

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [October 24, 2020, 1:49pm UTC](https://discourse.julialang.org/t/symbol-to-variable-in-a-macro/48924/7 "2020-10-24T13:49:23Z")

</div>

He he!!!

I will not go into that debate for now (I’ll try on my own first), but I’ll keep your question in mind:

You and others helped me a lot in the past weeks when I was working on a macro returning @generated functions. Once all the technicalities of doing that were painstakingly sorted out, someone (and quite possibly you) noted “you don’t need a generated function for that”, which gave me a classic case of Zen-like enlightenment, seeing the power of the combination of method instantiation, data described in the Type and greedy compilation, allowing apparently naive code (a function containing tests on the type of the input) resulting in very efficient compiled code. 😄

Julia rocks.
