For this specific case, why you do not use enumeration? Do you expect users to extend your abstract type?
Perceive that the cost is inescapable, if you used an @Enum
or an Int
directly to distinguish between the singleton types, then when you wanted to dispatch on these values you would need to wrap them in Val(value)
. So any code that wants to dispatch on a a field that may store different types (disguised or not) needs to pay the price of type instability. The only ways out I see are:
-
Do not have a single struct with a field that can hold multiple distinct types, have a parametrized struct instead. This is, use
mutable struct MyStruct1{consin <: AbstractType}; x :: Int; end
. Dispatch on the type parameter, that will be type stable because it is always the same for the same instance of your parametric type. - Use an implicit or explicit enumeration and then
Val
with the ValSplit package.