Parametric type signature in methods, any actual difference?

Was just curious if there was actually a difference in some way between these two ways of defining a method with parametric types.

function f1(x::Vector{<:Real})
    # Stuff
end
function f2(x::Vector{T}) where T <: Real
    # Stuff
end

Gave a quick try where i set # Stuff to be first(x) and looked at @code_native which seemed exactly the same, but maybe that was just so simple so it turned out the same?

1 Like

The difference lies in T - in the first version, you don’t have access to the type parameter itself (should you need it anywhere and the type given doesn’t have a convenient eltype defined) whereas the latter gives you access to the actual type as T.

Performance and compile wise they are the same, if you don’t need access to T directly.

4 Likes

Okay, exactly what I was wondering. I understood that access to T was one thing, but didn’t know if there were some additional compiler things or similar that made them different. Thanks!

There are a few cases in which the type parameter triggers a heuristic that tells Julia to specialize on the argument when it might not otherwise: https://docs.julialang.org/en/v1/manual/performance-tips/#Be-aware-of-when-Julia-avoids-specializing (doesn’t matter for Vector though).

3 Likes