Adding method to function in macro

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 (Symbols 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:

                   (::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> 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)
2 Likes