Thoughts around ways of ensuring compile time evaluation / const prop / inference?

I got my macro working: here it is

using MacroTools: splitdef, combinedef

macro checked(fdef)
    d = splitdef(fdef)

    f = d[:name]
    args = d[:args]
    whereparams = d[:whereparams]

    d[:name] = gensym()
    shadow_fdef = combinedef(d)
    M = __module__

    quote
        $shadow_fdef
        @generated function $f($(args...)) where {$(whereparams...)}
            d = $d
            T = Tuple{$(args...)}
            out_type = Core.Compiler.return_type($(d[:name]), Tuple{$(args...)})
            Core.println("statically inferred return type was $out_type")
            args = $args
            :($(d[:name])($(args...)))
        end
    end |> esc
end

Now at the REPL

julia> @checked f(x, y) = x + y
f (generic function with 1 method)

julia> f(1, 2)
statically inferred return type was Int64
3

julia> f(1, 2)
3
9 Likes