带符号的通用调度

时间:2016-09-04 08:21:32

标签: julia multiple-dispatch

我想知道是否有一种方法可以使用符号进行多次调度,还包括“全能方法”。即

之类的东西
function dispatchtest{alg<:Symbol}(T::Type{Val{alg}})
  println("This is the generic dispatch. The algorithm is $alg")
end
function dispatchtest(T::Type{Val{:Euler}})
  println("This is for the Euler algorithm!")
end

第二个工作并匹配手册中的内容,我只是想知道你是如何让第一个工作的。

1 个答案:

答案 0 :(得分:7)

你可以这样做:

julia> function dispatchtest{alg}(::Type{Val{alg}})
           println("This is the generic dispatch. The algorithm is $alg")
       end
dispatchtest (generic function with 1 method)

julia> dispatchtest(alg::Symbol) = dispatchtest(Val{alg})
dispatchtest (generic function with 2 methods)

julia> function dispatchtest(::Type{Val{:Euler}})
           println("This is for the Euler algorithm!")
       end
dispatchtest (generic function with 3 methods)

julia> dispatchtest(:Foo)
This is the generic dispatch. The algorithm is Foo

julia> dispatchtest(:Euler)
This is for the Euler algorithm!