I’ve had thoughts on how to encode tagged data that would be compatible with our current strategies for generating compact enum representations.
For example, if we added singleton types like Singleton<const T> (where the generic parameter is a constant of type T, for any type T), where such a type has just one inhabitant (the constant itself), then it seems to me like one could directly encode enums with explicit tags like so:
// assume 64-bit ptr with 8 byte alignment
enum Value<T> {
I32Val(i32, Singleton<1u32>),
ChrVal(char, Singleton<2u32>),
NulVal(Singleton<3u64>),
PtrVal(Box<T>), // (requires 0b00 suffix on ptrs)
}
This is, I think, a different take on some of the recent RFC’s that have suggested having some way to suggest that certain bit patterns are “invalid” for a particular type, as a way to let libraries encode the representation optimization that we apply to Option.
This design would require some other changes to the language to be really useful (e.g. if you wanted, atop a 32-bit architecture, to support 30-bit fixnums with a 2-bit type tag, then we would need to generalize our numerics to support i30/u30 and i2/u2, etc, as well as a way to ask for a packed representation if we don’t have that already). Still, it is something I’ve been musing about.