How to determine type of result from function in Julia without performing the computation

In Julia, is there a way to get the type of a function's return value given its arguments (or only the types of its arguments), without running the function? For instance, I would like to do this:

function foo(a, b)
    RetType = typeof(f(a, b))
    # do something

The problem here is that I actually called f on a and b to get the return type. Is there a way to avoid the function call? Something along the lines of promote_type or related functions?


  • You can define the following function:

    gettype(f, a, b) = last(@code_typed f(a, b))

    Now you can do:

    julia> gettype(+, 1, 2)
    julia> gettype(+, 1, 2.)
    julia> gettype(append!, Int[], 3)
    Vector{Int64} (alias for Array{Int64, 1})