# Not-so-unusual Macro issues

**URL:** <https://discourse.julialang.org/t/not-so-unusual-macro-issues/61570>\
**Category:** General Usage\
**Tags:** macros, metaprogramming\
**Created:** [May 21, 2021, 9:45am UTC](https://discourse.julialang.org/t/not-so-unusual-macro-issues/61570 "2021-05-21T09:45:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![harris-chris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/harris-chris/32/23877_2.png) [@harris-chris](https://discourse.julialang.org/u/harris-chris)\
**Post date:** [May 21, 2021, 9:45am UTC](https://discourse.julialang.org/t/not-so-unusual-macro-issues/61570/1 "2021-05-21T09:45:26Z")

</div>

Hello all. I’m having some macro-related issues. I’d like, if it’s even possible, to store the Expression of a function defined by the user, so I can write it to a file and use it elsewhere. I imagined that this would be most ergonomic for the user if it worked something like this, if it’s even possible:

```julia
module Test

mutable struct StoresFunction
    stored_func::Union{Nothing, Expr}
end

macro capture_function(sf, f)
  return quote
    $(esc(sf)).stored_func = $(esc(f))
  end
end

end

```

then from the point where this is called:

```julia
sf = Test.StoresFunction(nothing)

Test.@capture_function sf function tf(i::Int) i + 1 end

```

Which, ideally - though sadly not in this reality - stores the function expression down in the `sf` variable.

I feel that this might be quite close to working, but the compiler has no respect for my notions of ‘close to working’ and tells me:  
`MethodError: Cannot `convert` an object of type typeof(tf) to an object of type Expr`  
I have spent about two hours in trial and error, more or less randomly inserting `$` and `esc` all over the place. Could anyone who actually understands what they are doing please lend a hand?

---

<div class="post-metadata">

**Author:** ![sostock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sostock/32/5546_2.png) [@sostock](https://discourse.julialang.org/u/sostock)\
**Post date:** [May 21, 2021, 11:53am UTC](https://discourse.julialang.org/t/not-so-unusual-macro-issues/61570/2 "2021-05-21T11:53:47Z")

</div>

> [@harris-chris](#):
>
> `$(esc(sf)).stored_func = $(esc(f))`

`$(esc(f))` evaluates the expression `f`, so it is not an expression anymore, but a function. To avoid this, you can build the `sf.stored_func = f` expression yourself:

```julia
macro capture_function(sf, f)
    Expr(:(=), :($(esc(sf)).stored_func), Expr(:quote, f))
end

```

---

<div class="post-metadata">

**Author:** ![harris-chris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/harris-chris/32/23877_2.png) [@harris-chris](https://discourse.julialang.org/u/harris-chris)\
**Post date:** [May 22, 2021, 1:36am UTC](https://discourse.julialang.org/t/not-so-unusual-macro-issues/61570/3 "2021-05-22T01:36:41Z")

</div>

Thank you. That works great!
