Generic dispatch with Symbols

后端 未结 1 525
醉话见心
醉话见心 2021-02-09 07:03

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



        
相关标签:
1条回答
  • 2021-02-09 07:18

    You can do it this way:

    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!
    
    0 讨论(0)
提交回复
热议问题