# Interpolate a function in an Expr (macro) in called module scope

**URL:** <https://discourse.julialang.org/t/interpolate-a-function-in-an-expr-macro-in-called-module-scope/88952>\
**Category:** General Usage\
**Created:** [October 19, 2022, 9:34am UTC](https://discourse.julialang.org/t/interpolate-a-function-in-an-expr-macro-in-called-module-scope/88952 "2022-10-19T09:34:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![circonflexe](https://avatars.discourse-cdn.com/v4/letter/c/c57346/32.png) [@circonflexe](https://discourse.julialang.org/u/circonflexe)\
**Post date:** [October 19, 2022, 9:34am UTC](https://discourse.julialang.org/t/interpolate-a-function-in-an-expr-macro-in-called-module-scope/88952/1 "2022-10-19T09:34:04Z")

</div>

Here is some (heavily simplified) code:

```julia
module M
function foobar end
macro define_foobar_method()
  :($foobar() = 1)
end
end # module M

```

Note that the function `foobar` is _not_ exported by the module `M`; I would like the macro to expand to a definition of a new method for that function (hence requiring the use of the fully-qualified function name in the method definition). However, the above code fails with

```
syntax: invalid function name "M.define_foobar_method"

```

By inspecting the returned expression with `@macroexpand` I find that indeed the function definition expression contains

```
1: Expr
  head: symbol call
  args: Array{Any}((1,))
    1: define_foobar_method (function of type typeof(M.define_foobar_method))

```

In other words, the correct function is interpolated into the `Expr`, but then “re-parsing” the function name when evaluating the `Expr` fails. (I would qualify this as a bug: the `Function` member in the `Expr` is well defined; thus there exists one and only one reasonable way of evaluating such an `Expr`, which is using whichever name refers to that function in the current evaluation scope.)

Something that _seems_ to work however is to interpolate the _module_ itself:

```
:($(@ __MODULE__ ).foobar() = 1) # @ __MODULE__ evaluated as `M` here

```

Is this a correct solution? Does there exist a simpler one? In particular, the “real case” for such an expression would be parametrized by the function itself, and needing to separate the module from the function name means that the code now needs to determine which module the function belongs to (whereas the failed attempt above had the advantage of making this transparent).

---

<div class="post-metadata">

**Author:** ![dylanxyz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dylanxyz/32/36646_2.png) [@dylanxyz](https://discourse.julialang.org/u/dylanxyz)\
**Post date:** [October 19, 2022, 10:02am UTC](https://discourse.julialang.org/t/interpolate-a-function-in-an-expr-macro-in-called-module-scope/88952/2 "2022-10-19T10:02:20Z")

</div>

You don’t need to interpolate the function nor use `@ __MODULE__ `:

```julia
julia> module M
           function foobar end

           macro define_foobar_method()
               :(foobar() = 1)
           end
       end
Main.M

julia> M.foobar()
ERROR: MethodError: no method matching foobar()
Stacktrace:
 [1] top-level scope
   @ REPL[2]:1

julia> M.@define_foobar_method
foobar (generic function with 1 method)

julia> M.foobar()
1

```
