# How to create a type from Symbol in a macro

**URL:** https://discourse.julialang.org/t/how-to-create-a-type-from-symbol-in-a-macro/8749
**Category:** General Usage
**Created:** [February 1, 2018, 6:17pm UTC](https://discourse.julialang.org/t/how-to-create-a-type-from-symbol-in-a-macro/8749 "2018-02-01T18:17:21Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)
#### Post date: [February 1, 2018, 6:17pm UTC](https://discourse.julialang.org/t/how-to-create-a-type-from-symbol-in-a-macro/8749/1 "2018-02-01T18:17:21Z")

</div>

```nohighlight

module X
   export MM
   struct MM
   end
end
using X
Base.@pure get_module(::Type{T}) where T = T.name.module

macro mod_check(sym)
     amod = get_module(Type(sym))
end

@mod_check MM

```

My problem is to create a type from Symbol in the macro?  
How can i do that?  
Thanks

---

<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, 6:22pm UTC](https://discourse.julialang.org/t/how-to-create-a-type-from-symbol-in-a-macro/8749/2 "2018-02-01T18:22:41Z")

</div>

> [@Qiyamah](#):
>
> My problem is to create a type from Symbol in the macro?

`eval`, but you should really be retuning expressions from macros instead of values.

---

<div class="post-metadata">

### Author: ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)
#### Post date: [February 1, 2018, 6:24pm UTC](https://discourse.julialang.org/t/how-to-create-a-type-from-symbol-in-a-macro/8749/3 "2018-02-01T18:24:19Z")

</div>

Any side effect of eval for using Type Creation from Symbol?  
What should i be aware of?  
Thanks

---

<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, 6:28pm UTC](https://discourse.julialang.org/t/how-to-create-a-type-from-symbol-in-a-macro/8749/4 "2018-02-01T18:28:23Z")

</div>

> [@Qiyamah](#):
>
> Any side effect of eval for using Type Creation from Symbol?
> 
> What should i be aware of?

Why do you want this to be a macro?

Macros are called at compile-time. Functions are compiled once with only the types of the arguments known at compile-time, not the values. The global scope would have the values. So if what you’re evaling is based on the values of things, I would expect this to fail if you put it in a function. Things like that will trip you up. You really should only use macros to generate expressions, and use functions to return values.

---

<div class="post-metadata">

### Author: ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)
#### Post date: [February 1, 2018, 6:34pm UTC](https://discourse.julialang.org/t/how-to-create-a-type-from-symbol-in-a-macro/8749/5 "2018-02-01T18:34:09Z")

</div>

I am developing a trait system where i parse some super traits(more than one), here is an example:

```nohighlight
@mixin MyMixin{T,N}(ST2,ST3) <: ST1
  #stuff goes here
end

```

I look up info for super traits like ST1 from a toptable which makes addressing using module info and then the name of the type. You can think of it as:

toptable[MODULE][NAME] , eg: toptable[XX][ST1]

In the mixin macro, i need to have access to the module where ST1 is defined. That’s why.  
Returning a value is for the sake of a simple example, i am using the value in mixin macro.  
Thanks

---

<div class="post-metadata">

### Author: ![NaOH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/naoh/32/5632_2.png) [@NaOH](https://discourse.julialang.org/u/NaOH)
#### Post date: [February 2, 2018, 12:45am UTC](https://discourse.julialang.org/t/how-to-create-a-type-from-symbol-in-a-macro/8749/6 "2018-02-02T00:45:14Z")

</div>

You need to return an escaped expression that does what you want at run-time:

```julia
julia> macro mod_check(sym::Symbol)
            :(get_module(Type($sym))) |> esc
       end
@mod_check (macro with 1 method)

julia> @mod_check MM
X

```

---

<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 2, 2018, 3:12am UTC](https://discourse.julialang.org/t/how-to-create-a-type-from-symbol-in-a-macro/8749/7 "2018-02-02T03:12:28Z")

</div>

Don’t esc the whole thing. `esc` only user input.

---

<div class="post-metadata">

### Author: ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)
#### Post date: [February 5, 2018, 10:51pm UTC](https://discourse.julialang.org/t/how-to-create-a-type-from-symbol-in-a-macro/8749/8 "2018-02-05T22:51:14Z")

</div>

I was trying to find it in compile time (macro time) . That’s why i was using a pure function.  
Thanks
