This post was originally asked at 参数化方法的疑问 - 语言特性及语义 - Julia中文社区
Based on the doc at Methods · The Julia Language :
julia> function myappend(v::Vector{T}, x::T) where {T}
return [v..., x]
end
myappend (generic function with 1 method)
The type parameter
T
in this example ensures that the added elementx
is a subtype of the existing eltype of the vectorv
Thewhere
keyword introduces a list of those constraints after the method signature definition.
But what exactly are those constraints?
At first thought, I expect the second T
should be the subtype of the first T
. But based on my experiment with the following definition:
julia> function myappend(x::T,v::Vector{T}) where {T}
return [v..., x]
end
julia> myappend(true, Real[1, 2.0])
3-element Vector{Float64}:
1.0
2.0
1.0
It seems that we do not have such constraint in the order.
So now I’m also feeling confused