# Calling macro from within generated function in Julia

**URL:** <https://discourse.julialang.org/t/calling-macro-from-within-generated-function-in-julia/83632>\
**Category:** General Usage\
**Tags:** macros, metaprogramming\
**Created:** [July 1, 2022, 4:01pm UTC](https://discourse.julialang.org/t/calling-macro-from-within-generated-function-in-julia/83632 "2022-07-01T16:01:43Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![JordiManyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jordimanyer/32/37585_2.png) [@JordiManyer](https://discourse.julialang.org/u/JordiManyer)\
**Post date:** [July 1, 2022, 4:01pm UTC](https://discourse.julialang.org/t/calling-macro-from-within-generated-function-in-julia/83632/1 "2022-07-01T16:01:43Z")

</div>

I have been messing around with generated functions in Julia, and have come to a weird problem I do not understand fully: My final goal would involve calling a macro (more specifically `@tullio`) from within a generated function (to perform some tensor contractions that depend on the input tensors). But I have been having problems, which I narrowed down to calling the macro from within the generated function.

To illustrate the problem, let’s consider a very simple example that also fails:

```julia
macro my_add(a,b) 
    return :($a + $b)
end

function add_one_expr(x::T) where T
    y = one(T)
    return :( @my_add($x,$y) )
end

@generated function add_one_gen(x::T) where T
    y = one(T)
    return :( @my_add($x,$y) )
end

```

With these declarations, I find that `eval(add_one_expr(2.0))` works just as expected and returns and expression

```julia
:(@my_add 2.0 1.0)

```

which correctly evaluates to `3.0`.

However evaluating `add_one_gen(2.0)` returns the following error:

```julia
MethodError: no method matching +(::Type{Float64}, ::Float64)

```

Doing some research, I have found that `@generated` actually produces two codes, and in one only the types of the variables can be used. I think this is what is happening here, but I do not understand what is happening at all. It must be some weird interaction between macros and generated functions.

Can someone explain and/or propose a solution? Thank you!
