带有符号的通用调度 [英] Generic dispatch with Symbols

查看:12
本文介绍了带有符号的通用调度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以使用符号进行多次分派,但还包括一个包罗万象的方法".即类似

I was wondering if there was a way to use Symbols for multiple dispatch, but also include a "catch-all method". i.e. something like

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

第二个有效并且与手册中的内容相匹配,我只是想知道您如何使第一个有效.

The second one works and matches what's in the manual, I'm just wondering how you get the first one to work.

推荐答案

你可以这样:

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!

这篇关于带有符号的通用调度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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