# Get the actual type from an Expr representing a type

**URL:** <https://discourse.julialang.org/t/get-the-actual-type-from-an-expr-representing-a-type/113848>\
**Category:** General Usage\
**Tags:** metaprogramming\
**Created:** [May 4, 2024, 11:08pm UTC](https://discourse.julialang.org/t/get-the-actual-type-from-an-expr-representing-a-type/113848 "2024-05-04T23:08:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)\
**Post date:** [May 4, 2024, 11:08pm UTC](https://discourse.julialang.org/t/get-the-actual-type-from-an-expr-representing-a-type/113848/1 "2024-05-04T23:08:25Z")

</div>

I think this may be the question being asked [here](https://discourse.julialang.org/t/how-to-get-the-type-of-a-symbol-in-a-macro/12016/8), but it wasn’t resolved.

I am writing a macro that manipulates the signature of a function. I want to know if an argument is annotated with a subtype of a type owned by my package. Basic example:

```julia
using Expronicon

macro my_macro(func_expr)
    func = JLFunction(func_expr)
    type_annotation_expr = func.args[1].args[2]
    
     actual_type = # ?? Some logic to turn this into literal Vector{Int}?

    if actual_type <: Array
        print("Got a subtype!")
    end
    return nothing
end
@my_macro function f(x::Vector{Int}) end
# prints "Got a subtype!"

```

If it was unparameterized, I could use `getfield( __module__ , type_annotation_expr)`, but if it is parameterized, then that doesn’t work.

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [May 4, 2024, 11:41pm UTC](https://discourse.julialang.org/t/get-the-actual-type-from-an-expr-representing-a-type/113848/2 "2024-05-04T23:41:58Z")

</div>

Macros work at the level of expressions and should not be messing with their meaning, e.g., care which types or values an expression represents. They can generate an expression containing an appropriate condition though:

```julia
macro my_macro(func_expr)
    func = JLFunction(func_expr)
    type_annotation_expr = func.args[1].args[2]
           
    quote 
        if $(esc(type_annotation_expr)) <: Array
            print("Got a subtype!")
            # Insert expansion suitable for subtypes here ...
        else
            # Insert alternative expansion here ...
            nothing
        end
    end
end

```

```julia-repl
julia> @my_macro function f(x::Vector{Int}) end
Got a subtype!
julia> @my_macro function g(x::Real) end

```

---

<div class="post-metadata">

**Author:** ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)\
**Post date:** [May 4, 2024, 11:42pm UTC](https://discourse.julialang.org/t/get-the-actual-type-from-an-expr-representing-a-type/113848/3 "2024-05-04T23:42:50Z")

</div>

Ah! I feel silly for not thinking of that. Thank you!
