# Adding method to function in macro

**URL:** <https://discourse.julialang.org/t/adding-method-to-function-in-macro/128613>\
**Category:** General Usage\
**Tags:** macros, methods\
**Created:** [May 2, 2025, 7:44am UTC](https://discourse.julialang.org/t/adding-method-to-function-in-macro/128613 "2025-05-02T07:44:30Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [May 2, 2025, 8:49am UTC](https://discourse.julialang.org/t/adding-method-to-function-in-macro/128613/6 "2025-05-02T08:49:29Z")

</div>

Update: It only throws that invalid name error for the named method definition; comment that and its associated call out, and the other call works. Interpolating just a function object where it expects a `Symbol` possibly qualified by modules (`Symbol`s or not) results in a wrong name. I don’t know exactly how it’s wrong because defining a function with `var"MyModule.my_function"` is allowed but results in a separate function with a weird name that has nothing to do with `MyModule`. If you really want to interpolate the function object directly into the macro’s `Expr` instead of the module followed by some symbols, then you need the type-annotated functor syntax:

```julia
                   (::typeof($my_function))(::String) = -1
                   "" |> $my_function |> println

```

which just makes me wonder why `$my_function` in the name position can’t do the same thing.

MWE of this interpolation limitation for method definition names, no macros just eval:

```julia
julia> function foo end # motivation: foo or Main may not exist in macro call scope
foo (generic function with 0 methods)

julia> eval(:($foo()=0))
ERROR: syntax: invalid function name "Main.foo"
...
julia> eval(:($(@ __MODULE__ ).foo()=0))

julia> eval(:((::typeof($foo))(x)=1))

julia> foo(), foo("bar")
(0, 1)

```

---

_[View the full topic](https://discourse.julialang.org/t/adding-method-to-function-in-macro/128613)._
