Search code examples
functionperformancejuliadispatch

In Julia what's the difference between dispatching on abstract types versus parametric subset of abstract types?


Are there functional or performance differences between

  • myfunction(x::Real), and
  • myfunction(x::T) where {T<:Real}?

In this case, Real is an abstract type which obviously has concrete subtypes like Float64 and Int.

Are there reasons to prefer one versus the other?


Solution

  • The biggest difference is that you can refer to T in the function definition. The other difference is that for Functions and Varargs (but no other types, myfunction(x::T) where {T} forces specialization.

    Other than that, they are exactly the same.