以下是设计使然吗?
ASCIIString <: String
返回true
,但是Array{ASCIIString,1} <: Array{String,1}
返回false
.
也许这是一个错误,我应该在 github 上发出拉取请求。 (我运行的是 v0.4.0-dev+6210)
最佳答案
只需将我的评论转换为答案即可:
你的问题的答案是肯定的。正如 Julia 的document说:
in the parlance of type theory, Julia’s type parameters are invariant, rather than being covariant (or even contravariant).
在下面的误导性案例中,人们可能会发现
julia> Int64 <: Int
true
julia> Array{Int64,1} <: Array{Int,1}
true
这是因为 Int
和 Int64
是相同的类型。
julia> xdump(Int)
Int64::DataType <: Signed
julia> xdump(Int64)
Int64::DataType <: Signed
julia> xdump(Array{Int,1})
Array{Int64,1}::DataType <: DenseArray{Int64,1}
julia> xdump(Array{Int64,1})
Array{Int64,1}::DataType <: DenseArray{Int64,1}
我们可以使用typejoin
来检查类型A
和类型B
之间的关系以及它们对应的“数组”。
julia> typejoin(Int64,Int)
Int64
julia> typejoin(Array{Int64,1},Array{Int,1})
Array{Int64,1}
但是
julia> typejoin(ASCIIString,String)
String
julia> typejoin(Array{String,1},Array{ASCIIString,1})
Array{T,N} (constructor with 9 methods)
关于数组破坏了 Julia 中的字符串类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31691792/