# @generated inside macro: who's interpolating?

**URL:** <https://discourse.julialang.org/t/generated-inside-macro-whos-interpolating/46895>\
**Category:** General Usage\
**Created:** [September 19, 2020, 4:52am UTC](https://discourse.julialang.org/t/generated-inside-macro-whos-interpolating/46895 "2020-09-19T04:52:11Z")\
**Posts on this page:** 1\
**Showing post:** 14

<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:** [September 26, 2020, 4:34am UTC](https://discourse.julialang.org/t/generated-inside-macro-whos-interpolating/46895/14 "2020-09-26T04:34:06Z")

</div>

Well, I think in the end I must bow to Cedric cstjean’s hard earned wisdom.

As I see it the main issue is that with a @generated -in-a-macro, where some of the interpolation is intended to occur at macro time, some at generation time, the code I was trying to write is not explicit enough on this decision: even if we got the code to work (and I still struggle with this :a vs. :(:a) business in spite of Tom’s good remark), it’s not readable. At least I get a headache reading my own code. So I got it all to work with the readable, is somewhat verbose, code:

```julia
Base.replace(ex::Symbol,a::Symbol,b) = ex==a ? deepcopy(b) : ex
Base.replace(ex::Expr ,a::Symbol,b) = Expr(ex.head,replace.(ex.args,a,Ref(b))...)
Base.replace(ex ,a::Symbol,b) = ex
macro mac(A)
    ex = quote
        @generated jenny(a,b) = :(a+b*øa)
        foo(a,b) = a+b*øb
    end
    ex = replace(ex,:øa,A)
    ex = replace(ex,:øb,A)
    return esc(ex)
end

@mac(a)
jenny(2.,3.)
foo(1.,2.)

```

Here I explicitely prescribe the interpolations that are to occur at macro time - and I could have done the same within the @generate function. Flexible, explicit, and so far, works.

A bit of syntax sweetening on the replace function and the job is done.

Neat thing with a Scandinavian keyboard: øReplaceMe 😉

---

_[View the full topic](https://discourse.julialang.org/t/generated-inside-macro-whos-interpolating/46895)._
