The fact that you can have an Enum which can contain a value like the example below makes so much difference.
This has existed for many years in the functional programming space. Haskell has them (called sum types) and F# (called discriminated union), and I believe it has even made its way into C# now (I'm not really keeping up with C# anymore).
But I totally agree. This is such an amazing feature, along with a match statement (or similar) to deconstruct the enum. When coming from a language that has it to a language that doesn't I would constantly miss it.
As the name implies, you can implement them yourself in C/C++. It basically consists of three things: an enum that lists out the constructors (Type1, Type2, and Type 3, in OP's example); a union over the types being summed (String, u32, and unit); and a struct containing the enum and the union. You then switch the enum to figure out what the underlying type is, and access the associated union case to get the data.
This is why they're called tagged unions or discriminated unions.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
This has existed for many years in the functional programming space. Haskell has them (called sum types) and F# (called discriminated union), and I believe it has even made its way into C# now (I'm not really keeping up with C# anymore).
But I totally agree. This is such an amazing feature, along with a
match
statement (or similar) to deconstruct the enum. When coming from a language that has it to a language that doesn't I would constantly miss it.As the name implies, you can implement them yourself in C/C++. It basically consists of three things: an enum that lists out the constructors (Type1, Type2, and Type 3, in OP's example); a union over the types being summed (String, u32, and unit); and a struct containing the enum and the union. You then switch the enum to figure out what the underlying type is, and access the associated union case to get the data.
This is why they're called tagged unions or discriminated unions.