带有符号的通用调度
Generic dispatch with Symbols
我想知道是否有一种方法可以使用 Symbols 进行多次分派,而且还包括一个 "catch-all method"。即类似
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
第二个有效并且与手册中的内容相匹配,我只是想知道你是如何让第一个有效的。
你可以这样做:
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!
我想知道是否有一种方法可以使用 Symbols 进行多次分派,而且还包括一个 "catch-all method"。即类似
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
第二个有效并且与手册中的内容相匹配,我只是想知道你是如何让第一个有效的。
你可以这样做:
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!