happy new year!
I have some confusion around the meaning of :: and <:. Please take a look at the following:
function f(x::Real)
println("f ", x)
end
function g1(x::Vector{Real})
println("g1 ", x)
end
function g2(x::Vector{<:Real})
println("g2 ", x)
end
julia> f(3)
f 3
julia> f(3.2)
f 3.2
julia> g1([3,2])
ERROR: MethodError: no method matching g1(::Array{Int64,1})
julia> g1([3.0,2.0])
ERROR: MethodError: no method matching g1(::Array{Float64,1})
julia> g2([3,2])
g2 [3, 2]
julia> g2([3.0,2.0])
g2 [3.0, 2.0]
We use :: in f() to specify x is a subtype of Real (NOT the type of Real).
While in g2(), we need to use <: (rather than :: ) to specify that the parameter (of the parametric type) is a subtype of Real.
Note that calling g1() always fails.
I have confusion about these notations.
Is it better to fix the meaning of :: to be “the type of”, and <: to be “the subtype of” ? If it’s the case, the following should be allowed (but not now):
julia> function f2(x<:Real)
println("f ", x)
end
ERROR: syntax: "x <: Real" is not a valid function argument name