Part of what makes it so “light” is that you can just use dispatch, i.e.
f(::A) = 1
f(::B) = 2
f(variant(x))
Could probably turn that into a macro pretty easily though. You’d then just use JET.jl to check for completeness by checking for the possibility of method errors.
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;