UTF8行为的Julia-Array
我遇到了一个我已经解决的问题,但是为什么这个解决方案对我来说没有意义 我有一个类似的函数UTF8行为的Julia-Array,julia,Julia,我遇到了一个我已经解决的问题,但是为什么这个解决方案对我来说没有意义 我有一个类似的函数 function testB(a::Array{AbstractString}) println(a) end 像这样运行它给了我 testB(convert(Array{UTF8String},["a","b"])) ERROR: MethodError: `testB` has no method matching testB(::Array{UTF8String,1}) 请注意,实
function testB(a::Array{AbstractString})
println(a)
end
像这样运行它给了我
testB(convert(Array{UTF8String},["a","b"]))
ERROR: MethodError: `testB` has no method matching
testB(::Array{UTF8String,1})
请注意,实际上我并没有手动转换为UTF8,这是为了演示,实际上我有一个AbstractString数组,但当我从中提取元素时,它们变成了UFT8
我的解决方案简而言之
function testA{T <: AbstractString}(a::Array{T})
println(a)
end
有人能告诉我为什么testA工作而testB不工作吗?
另外,当
UTF8String
是AbstractString
的子类型时,Array{UTF8String}
不是Array{AbstractString}
的子类型(无协方差)。因此您的testB不工作。(但是testB(convert(Array{AbstractString},[“a”,“b”)
应该工作。)
为什么它必须是这样的理由:一个函数f(x::Vector{AbstractString})
可以例如push!
将一个新的FooString
推入x
(假设FooString
是AbstractString
的一个子类型)。现在如果x
实际上是Vector{UTF8String}/code>,那就失败了
testA(convert(Array{UTF8String},["a","b"]))
UTF8String["a","b"]