4 ms·
Good point. For example, type NotAProduct = Bool | Bool only has 2 values, not 4. For a real product type, we need type Product = {tag: "Left", value
by lalaithion 5y ago
Good point. For example,
type NotAProduct = Bool | Bool
only has 2 values, not 4. For a real product type, we need
type Product = {tag: "Left", value: Bool} | {tag: "Right", value: Bool}
which has the requisite 4 values.
- feoren 5y agoThis is also called a "discriminated union", with "tag" being the discriminator here.
- nyanpasu64 5y agoSo to replicate a Rust enum, C tagged union, or C++ std::variant containing one byte for the type and the rest of the object inline, JavaScript needs a boxed object wrapping a string and the actual object contained inside? And the engine can't optimize away the boxed object into an inline value type because it could have multiple aliasing references in multiple locations? Boxing-based languages make me sad.