# Creating symbols inside of macros

**URL:** <https://discourse.julialang.org/t/creating-symbols-inside-of-macros/5812>\
**Category:** General Usage\
**Tags:** question, macros\
**Created:** [September 10, 2017, 8:56pm UTC](https://discourse.julialang.org/t/creating-symbols-inside-of-macros/5812 "2017-09-10T20:56:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [September 10, 2017, 8:56pm UTC](https://discourse.julialang.org/t/creating-symbols-inside-of-macros/5812/1 "2017-09-10T20:56:20Z")

</div>

How to create symbols (and new types) inside of macros?

```julia
macro mymacro(arg)
  quote
    structname = Symbol($arg,"ext")
    struct structname
    end
  end
end

```

In this example, I am expecting a call `@mymacro "Foo"` to define a new struct type `Fooext`.

---

<div class="post-metadata">

**Author:** ![favba](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/favba/32/2735_2.png) [@favba](https://discourse.julialang.org/u/favba)\
**Post date:** [September 10, 2017, 9:06pm UTC](https://discourse.julialang.org/t/creating-symbols-inside-of-macros/5812/2 "2017-09-10T21:06:40Z")

</div>

I’m on my phone now, so I cannot test things. But `@macroexpand` is your friend to test things out. Try escaping your quote: `esc(quote ... end)`.  
This would be my guess

```julia
macro mymacro(arg)
    structname = Symbol($arg,"ext")
    esc(quote
      struct $structname
      end
  end)
end

```

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [September 10, 2017, 9:11pm UTC](https://discourse.julialang.org/t/creating-symbols-inside-of-macros/5812/3 "2017-09-10T21:11:20Z")

</div>

Hi @favba, I think the issue is somewhere else, aren’t macros supposed to return quotes?

---

<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 10, 2017, 9:27pm UTC](https://discourse.julialang.org/t/creating-symbols-inside-of-macros/5812/4 "2017-09-10T21:27:25Z")

</div>

> [@juliohm](#):
>
> aren’t macros supposed to return quotes?

No. Macros are supposed to return an expression based on the inputs (expressions) it gets. Most macros won’t just be a quote, there will be code outside it and not all of the processing will be done in the quote.

@favba is totally correct here though a slightly more preferred version would be

```julia
macro mymacro(arg)
    quote
        struct $(esc(Symbol(arg, "ext"))) # This is spliced inside the quote, you can of course hoist it into `structname` **OUTSIDE** the quote as in @favba's answer.
        end
    end
end

```
