Julia from the perspective of a pythonista

Probably that should be split in a new topic. But if you use AbstractVector your function will accept, for example, views, StaticArrays, and other array types which are not Vectors in the strict sense:

julia> f(x::Vector) = 1
f (generic function with 1 method)

julia> g(x::AbstractVector) = 1
g (generic function with 1 method)

julia> x = [1,2,3];

julia> f(@view x[1:2])
ERROR: MethodError: no method matching f(::SubArray{Int64,1,Array{Int64,1},Tuple{UnitRange{Int64}},true})
Closest candidates are:
  f(::Array{T,1} where T) at REPL[1]:1
Stacktrace:
 [1] top-level scope at REPL[3]:1

julia> g(@view x[1:2])
1

julia> using StaticArrays

julia> x = zeros(SVector{3,Float64});

julia> f(x)
ERROR: MethodError: no method matching f(::SArray{Tuple{3},Float64,1,3})
Closest candidates are:
  f(::Array{T,1} where T) at REPL[1]:1
Stacktrace:
 [1] top-level scope at REPL[8]:1

julia> g(x)
1


Concerning Real vs. <:Real I like my way to explain this, for obvious reasons.

1 Like