在向量的参数向量上调度时发生MethodError [英] MethodError when dispatching on a parametric vector of vectors

查看:127
本文介绍了在向量的参数向量上调度时发生MethodError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个函数,该函数在Integer s个向量的向量上分派.但是,当我尝试使用它时会出现MethodError:

I've written a function that dispatches on a vector of vectors of Integers. However, I get a MethodError when I try to use it:

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}的简写形式:

The notation here is a little subtle. The parametric type that you've declared for the x argument, Vector{Vector{<:Integer}} is a shorthand notation for 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.在前一种类型中,内部向量元素的具体整数类型对于每个内部向量可以不同.在后一种类型中,所有内部向量都具有相同的具体整数类型的元素.

Most importantly, note that Vector{Vector{T} where T<:Integer} is not equivalent to Vector{Vector{T}} where T<:Integer. In the former type, the concrete integer type of the inner vector elements can be different for each inner vector. In the latter type, all the inner vectors have elements of the same concrete integer type.

此外,实例化类型为Vector{Vector{T} where T<:Integer}的文字数组非常棘手,因为文字数组构造函数会提升其参数类型:

Furthermore, it is tricky to instantiate a literal array of type Vector{Vector{T} where T<:Integer}, because the literal array constructor promotes the types of its arguments:

julia> typeof([Int8(1), Int16(2)])
Array{Int16,1}

julia> typeof([Int8[1], Int16[2, 3]])
Array{Array{Int16,1},1}

但是,可以按照以下步骤完成

However, it can be done as follows,

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

我们广泛使用了键入数组初始值设定项.

或者,如果您希望每个内部数组的元素都具有相同的具体整数类型,则可以按以下方式定义函数:

Alternatively, if you're fine with requiring the elements of each inner array to have the same concrete integer type, you could define your function as follows:

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

请注意,如果具体整数类型不同,则此方法将不接受向量的向量:

Note that this method won't accept a vector of vectors where the concrete integer types are different:

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

有关的讨论,请参见

For a related discussion, see the section of the Julia manual on UnionAll types.

这篇关于在向量的参数向量上调度时发生MethodError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆