Julia多个分派不匹配的子类型

Julia多个分派不匹配的子类型,julia,Julia,有人能帮我理解为什么这个julia函数定义与我试图使用它的尝试不匹配吗 我天真的假设是,传递到函数中的数组{ascistring,1}应该与数组{AbstractString,1}的函数定义相匹配,依此类推 julia> function test(a::Array{AbstractString,1}, b::AbstractString, c::Any) end test (generic function with 1 method) julia> test([""],"","

有人能帮我理解为什么这个julia函数定义与我试图使用它的尝试不匹配吗

我天真的假设是,传递到函数中的
数组{ascistring,1}
应该与
数组{AbstractString,1}
的函数定义相匹配,依此类推

julia> function test(a::Array{AbstractString,1}, b::AbstractString, c::Any) end
test (generic function with 1 method)

julia> test([""],"","")
ERROR: MethodError: `test` has no method matching test(::Array{ASCIIString,1}, ::ASCIIString, ::ASCIIString)
Closest candidates are:
  test(::Array{AbstractString,1}, ::AbstractString, ::Any)

julia>

我认为把我上面的两个评论变成一个答案是值得的


正如@DanGetz所指出的,这里的重要短语是不变的。在这个特殊的例子中,这个原则意味着自从科林的回答之后,
ascistring,Julia的语法发生了变化。从Julia版本1开始,它如下所示:

function test(a::Array{T}, b::T, c::Any) where {T<:AbstractString}

函数测试(a::Array{T},b::T,c::Any),其中{T原因是Julia类型是不变的,这意味着
issubtype(Array{ascistring,1},Array{AbstractString,1})
为false。有关更多信息,请参阅。这是中问题的一个简单示例。但是,问题的表示形式不同,我认为我们可以将其保留。为了快速参考,您需要的语法是
函数测试{TOops,请忽略我关于“第二个类型参数”的胡说八道在前面的评论中。如果您想让
a
的元素类型和
b
的类型有所不同,那么
函数测试{t解释得很好,谢谢!对于其他关注这个问题的人,我必须查找一些内容:
eltype(…)
返回集合的类型,例如:
eltype(Array{ascistring,1})#Returns:ascistring
。此外,我发现这篇文章与此相关,读起来很有帮助:@DavidParks很乐意帮忙!
function test{T<:AbstractString}(a::Array{T}, b::AbstractString, c::Any)
function test(a::Array{T}, b::T, c::Any) where {T<:AbstractString}