Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Julia 是否可以对非a-type执行多个分派?_Julia_Multiple Dispatch - Fatal编程技术网

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::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)
    有没有像not-a-type这样的东西?我是不是想错了?有没有一种规范的方法来解决这类问题

    对于上下文,我正在尝试为我编写的结构实现Base.getindex,我希望getindex能够在结构的内容可能有所不同的情况下支持许多不同的方法对结构进行索引。在后台,结构中的元素由整数索引,但是用户可能使用几乎任意的非整数类型来索引结构中的元素(我不想强制用户使用特定类型来索引元素)

    您可以指定(Array,Int)大小写,然后添加不太具体的方法:

    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