Return types of function is Any[Float64], although I defined return type to be Float64

Hello everyone,
I have a function whose return type is defined and it’s Float64. However, when I check with Base.return_types(f), it says Any[Float64]. What does it mean and will it affect performance?

That’s good, it just means the only return type is Float64. From the docstring:

Return a list of possible return types for a given function f and argument types types.
The list corresponds to the results of type inference on all the possible method match
candidates for f and types (see also [methods(f, types)](@ref methods).

So if you give abstract types to match more methods, you’d see a larger array of types inferred from the types you provided or the argument annotations, depending on which is more specific:

julia> f(x::Any) = x+1; f(x::Float64) = x-0.1
f (generic function with 2 methods)

julia> Base.return_types(f, (Int,)) # infers f(::Int) for f(x::Any)
1-element Vector{Any}:

julia> Base.return_types(f, (Number,)) # f(::Number) for f(x::Any), f(::Float64) for f(x::Float64)
2-element Vector{Any}:

Just use @code_warntype or packages like JET.jl and Cthulhu.jl for a particular call signature, it’s more informative.


Thank you so much, I got confused because of Any. But now I see it refers to type of elements in the list, and I have only Float64 in the list.
Thank you for a fast reply!

1 Like