Hacker News new | ask | show | jobs
by lalaithion 1775 days 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.
2 comments

This is also called a "discriminated union", with "tag" being the discriminator here.
So 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.