3 ms·
They are not the same feature. Enums allows you to create new values, while union types only allow you to combine values. For example, in Rust, enum Toto { Titi
by Zababa 5y ago
They are not the same feature. Enums allows you to create new values, while union types only allow you to combine values. For example, in Rust, enum Toto { Titi, Tata } would create a new type, Toto, that contains two values, Toto::Titi and Toto::Tata. There's no way to express that with union types.
On the other hand, Rust's enum have to be defined before use: I can't suddently decide to have a function that returns "Toto" | "Tata" without creating a type or returning string.
OCaml has something called "polymorphic variants" that seem to be a middle-ground between the two: https://ocaml.org/manual/polyvariant.html https://ocaml.org/manual/polyvariant.html.