By using a struct with lazily initialized members, for example.
enum SumType {
Foo(foo),
Bar(bar),
Baz(baz),
}
void DoSomething(SumType sum_type) {
match sum_type {
Foo(foo): foo.do_x();
Bar(bar): bar.do_y();
Baz(baz): baz.do_z();
}
}
This could be lowered to: struct LoweredSumType {
optional<Foo> foo;
optional<Bar> bar;
optional<Baz> baz;
}
void DoSomething(LoweredSumType sum_type) {
if (sum_type.foo.has_value()) {
sum_type.foo->do_x();
} else if (sum_type.bar.has_value()) {
sum_type.bar->do_y();
} else {
sum_type.baz->do_z();
}
}
This isn't real code; it's a pretend language that's kind of a mix of Rust and C++. But it illustrates that the sum type can be lowered into a non-union representation.which looks remarkably like a union to me, if i understand your made-up language.