Create any functions with a macro, the functions can call each other

hello, i need to create any functions with a macro, the functions can call each other. What am I doing wrong ?

macro macr(ClassName::Symbol)
    local Class = @eval $ClassName
    return esc(quote
        foo(::Type{$ClassName}) = $(2 + 2)
        bar(::Type{$ClassName}) = $(foo(Class) + 2)
    end)
end

@macr Int  # -> **LoadError: UndefVarError: foo not defined**

Is this

macro macr(ClassName::Symbol)
    local Class = @eval $ClassName
    return esc(quote
        foo(::Type{$ClassName}) = $(2 + 2)
        bar(::Type{$ClassName}) = (foo($Class) + 2)
    end)
end

@macroexpand (@macr Int)

yielding

quote
    foo(::Type{Int}) = begin
            4
        end
    bar(::Type{Int}) = begin
            foo(Int64) + 2
        end
end

what you want?

1 Like

i want result of foo(Int64) + 2

quote
    foo(::Type{Int}) = begin
            4
        end
    bar(::Type{Int}) = begin
            6
        end
end
1 Like