SymbolDispatch

Documentation for SymbolDispatch.

It exports macros @symboldispatch and @symboldispatch_pos2

SymbolDispatch.@symboldispatch โ€” Macro
@symboldispatch foo(Val{:asymbol}, ...) = body

This macro takes a function definition whose first argument is a valuetype of a symbol and creates two more methods, a dispatch and a default fallback:

foo(s::Symbol, ...) = foo(Val(s), ...)

foo(::Val{T}, ...) where T = ArgumentError("Its defined for <possible symbols>")

It helps to implement the Singleton type dispatch pattern without exposing the usage of valuetypes to the user of the function. Depending on the given Symbol s, the respective method is invoked.

Note that additional methods of foo for other possible symbols should be defined without the macro in order to avoid redifinition of the dispatch and fallback method.

module mod_tmp
    using SymbolDispatch
    @symboldispatch _bar(::Val{:method1}) = "method for :method1"
    _bar(::Val{:method2}) = "method for :method2"
end
#mod_tmp._bar(:symbol_without_dispatch) # reporting :method1,:method2
mod_tmp._bar(:method1) == "method for :method1"
source
SymbolDispatch.@symboldispatch_pos2 โ€” Macro
@symboldispatch_pos2 foo(_, Val{:asymbol}, ...) = body

Similar to @symboldispatch, adds a a dispatch and a default method, but takes a function where the second argument is a valuetype of a symbol is used to dispatch.

foo(x1, s::Symbol, ...) = foo(x1, Val(s), ...)

foo(_, ::Val{T}, ...) where T = ArgumentError("Its defined for <possible symbols>")

This helps to support different dispatches of mutating methods, where the first argument is conventionally the mutated element.

module mod_tmp2
    using SymbolDispatch
    @symboldispatch_pos2 _bar!(x, ::Val{:method1}) = "method for :method1"
    _bar!(x, ::Val{:method2}) = "method for :method2"
end
#mod_tmp2._bar!([1], :symbol_without_dispatch) # reporting :method1,:method2
mod_tmp2._bar!([1], :method1) == "method for :method1"
source