我编写了一个函数,用于在整数向量组成的向量上进行分派(dispatch)。但是,当我尝试使用它时,我收到了 MethodError:
julia> foo(x::Vector{Vector{<:Integer}}) = last(last(x));
julia> x = [[1], [2, 3], [4, 5, 6]]
3-element Array{Array{Int64,1},1}:
[1]
[2, 3]
[4, 5, 6]
julia> foo(x)
ERROR: MethodError: no method matching foo(::Array{Array{Int64,1},1})
Closest candidates are:
foo(::Array{Array{#s17,1} where #s17<:Integer,1}) at REPL[1]:1
为什么这不起作用?
最佳答案
这里的符号有点微妙。您为 x
声明的参数类型参数,Vector{Vector{<:Integer}}
是 Vector{Vector{T} where T<:Integer}
的简写符号:
julia> Vector{Vector{<:Integer}}
Array{Array{#s17,1} where #s17<:Integer,1}
julia> Vector{Vector{T} where T<:Integer}
Array{Array{#s17,1} where #s17<:Integer,1}
最重要的是,请注意 Vector{Vector{T} where T<:Integer}
不等于Vector{Vector{T}} where T<:Integer
。在前一种类型中,每个内向量的内向量元素的具体整数类型可以不同。在后一种类型中,所有内部向量都具有相同具体整数类型的元素。
此外,实例化Vector{Vector{T} where T<:Integer}
类型的文字数组是很棘手的。 ,因为文字数组构造函数提升了其参数的类型:
julia> typeof([Int8(1), Int16(2)])
Array{Int16,1}
julia> typeof([Int8[1], Int16[2, 3]])
Array{Array{Int16,1},1}
但是,可以按如下方式完成,
julia> foo(x::Vector{Vector{<:Integer}}) = last(last(x));
julia> y = Vector{<:Integer}[Int8[1], Int16[2, 3], Int32[4, 5, 6]]
3-element Array{Array{#s17,1} where #s17<:Integer,1}:
Int8[1]
Int16[2, 3]
Int32[4, 5, 6]
julia> foo(y)
6
我们广泛使用了 typed array initializers .
或者,如果您愿意要求每个内部数组的元素具有相同的具体整数类型,则可以按如下方式定义函数:
julia> bar(x::Vector{Vector{T}}) where T<:Integer = last(last(x));
julia> x = [[1], [2, 3], [4, 5, 6]]
3-element Array{Array{Int64,1},1}:
[1]
[2, 3]
[4, 5, 6]
julia> bar(x)
6
请注意,此方法不会接受具体整数类型不同的向量向量:
julia> bar(y)
ERROR: MethodError: no method matching bar(::Array{Array{#s17,1} where #s17<:Integer,1})
Closest candidates are:
bar(::Array{Array{T,1},1}) where T<:Integer at REPL[35]:1
有关相关讨论,请参阅 Julia 手册中关于 UnionAll 的部分。类型。
关于julia - 在向量的参数向量上调度时出现 MethodError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59758161/