In Julia, can a macro access the inferred type of its arguments?

Macros cannot do this, but generated functions can.

Check out the docs here: https://docs.julialang.org/en/v1/manual/metaprogramming/#Generated-functions-1


In addition to spencerlyon2's answer, another option is to just generate explicit branches:

macro report_int(x)
    :(isa(x,Int64) ? "it's an int" : "not an int")
end

If @report_int(x) is used inside a function, and the type of x can be inferred, then the JIT will be able to optimise away the dead branch (this approach is used by the @evalpoly macro in the standard library).

Tags:

Julia