API

DynamicSumTypes.@sum_structsMacro
@sum_structs [version] type_definition begin
    structs_definitions
end

This macro allows to combine multiple types in a single one. The default version is :on_fields which has been built to yield a performance almost identical to having just one type. Using :on_types consumes less memory at the cost of being a bit slower.

Example

julia> @sum_structs AB begin
           struct A x::Int end
           struct B y::Int end
       end

julia> a = AB'.A(1)
AB'.A(1)

julia> a.x
1
DynamicSumTypes.@patternMacro
@pattern(function_definition)

This macro allows to pattern on types created by @sum_structs.

Notice that this only works when the kinds in the macro are not wrapped by any type containing them.

Example

julia> @sum_structs AB begin
           struct A x::Int end
           struct B y::Int end
       end

julia> @pattern f(::AB'.A) = 1;

julia> @pattern f(::AB'.B) = 2;

julia> @pattern f(::Vector{AB}) = 3; # this works 

julia> @pattern f(::Vector{AB'.B}) = 3; # this doesn't work
ERROR: LoadError: It is not possible to dispatch on a variant wrapped in another type
...

julia> f(AB'.A(0))
1

julia> f(AB'.B(0))
2

julia> f([AB'.A(0), AB'.B(0)])
3
DynamicSumTypes.@finalize_patternsMacro
@finalize_patterns

Calling @finalize_patterns is needed to define at some points all the functions @pattern constructed in that module.

If you don't need to call any of them before the functions are imported, you can just put a single invocation at the end of the module.

DynamicSumTypes.kindofFunction
kindof(instance)

Return a symbol representing the conceptual type of an instance:

julia> @sum_structs AB begin
           struct A x::Int end
           struct B y::Int end
       end

julia> a = AB'.A(1);

julia> kindof(a)
:A
DynamicSumTypes.allkindsFunction
allkinds(type)

Return a Tuple containing all kinds associated with the overarching type defined with @sum_structs

julia> @sum_structs AB begin
           struct A x::Int end
           struct B y::Int end
       end

julia> allkinds(AB)
(:A, :B)
DynamicSumTypes.export_variantsFunction
export_variants(T)

Export all variants types into the module the function it is called into.

Example

julia> @sum_structs AB begin
           struct A x::Int end
           struct B y::Int end
       end

julia> AB'.A(1)
AB'.A(1)

julia> export_variants(AB)

julia> A(1) # now this also works
AB'.A(1)
DynamicSumTypes.variant_constructorFunction
variant_constructor(instance)

Return the constructor of an instance in a more efficient way than doing typeof(inst)'[kindof(inst)]:

julia> @sum_structs AB begin
           struct A x::Int end
           struct B y::Int end
       end

julia> a = AB'.A(1)
AB'.A(1)

julia> typeof(a)'[kindof(a)]
AB'.A

julia> variant_constructor(a)
AB'.A