Julia 是否可以对非a-type执行多个分派?
为了简化,我尝试编写一个具有两个参数的函数,其中:Julia 是否可以对非a-type执行多个分派?,julia,multiple-dispatch,Julia,Multiple Dispatch,为了简化,我尝试编写一个具有两个参数的函数,其中: 基方法接受两个整数作为参数 func(x::Int,y::Int)=某物 其他方法接受任意类型的一个或两个参数,将这些参数映射到整数,并调用基方法 其他方法接受一个或两个参数作为数组(或::冒号类型),并通过应用适当的先前方法(1)或(2)elementwise生成数组 不出所料(事后看来),这种方法会产生方法歧义。给定提供给函数的参数类型,Julia选择具有最特定类型的有效方法。但如果x是数组,y是Int,则以下方法同样特定,Julia不知道
- func(x::Any,y::Int)
- func(x::Array,y::Any)
- func(x::T,y::Int)T func(el,y))(x)
julia> func(x::Array, i::Int) = 0
func (generic function with 1 method)
julia> func(x, i::Int) = 1
func (generic function with 2 methods)
julia> func(x::Array, i) = 2
func (generic function with 3 methods)
julia> methods(func)
# 3 methods for generic function "func":
[1] func(x::Array, i::Int64) in Main at REPL[1]:1
[2] func(x, i::Int64) in Main at REPL[2]:1
[3] func(x::Array, i) in Main at REPL[3]:1