General sending with characters

I was wondering if there is a way to use Symbols for multiple sending, but also include a "catch-all" method. that is, 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 corresponds to what is in the manual, I'm just wondering how you will get the first one.

+6
source share
1 answer

You can do it as follows:

 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! 
+7
source

All Articles