Simple sum types

I’ve done some light testing previously and it seems to work fine even with very large numbers of variants. E.g. here’s a benchmark on a sum type with 64 variants and the numbers indicate that it’s spending only one clock cycle per application of f:

julia> using LightSumTypes, Random

julia> var_vec = let variants = [Symbol(:A, i) for i ∈ 1:64]
           for (i, var) ∈ enumerate(variants)
               @eval begin
                   struct $var end
                   f(::$var) = $i
               end
           end
           @eval begin
               @sumtype S($(variants...),)
               f(s::S) = f(variant(s))
           end
           shuffle!([S(getproperty(Main, var)()) for var ∈ variants])
       end;
julia> @benchmark sum(f, $var_vec)
BenchmarkTools.Trial: 10000 samples with 990 evaluations.
 Range (min … max):  42.151 ns … 378.326 ns  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     43.466 ns               ┊ GC (median):    0.00%
 Time  (mean ± σ):   44.029 ns ±   9.632 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

        ▃█▅                                                     
  ▂▄▂▂▃▆███▇▅▃▃▂▂▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▁▂▂▂▂▂▂▁▂▂ ▃
  42.2 ns         Histogram: frequency by time         52.3 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.
5 Likes