Re:Switching from C++ to Rust
by cornstalks on Monday March 13, 2023 @09:07PM
(#35145891)
Pretend we have the following sum type:
This could be lowered to:
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.
[ Parent
| on HN ]
> Pretend we have the following sum type:
which looks remarkably like a union to me, if i understand your made-up language.
It's not a union, though, it stores references to three memory locations, not just one (as a union represents a single memory location which can have multiple possible interpretations). And the lowered code has nothing protecting it from an invariant where more than one reference is non-null!