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
function f2(x::Vector{T}) where T <: Real
    # Stuff

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.


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: (doesn’t matter for Vector though).