1
NeoLemmix Main / Re: Don't exit on losing all lemmings (feature development)
« on: May 14, 2024, 04:47:31 AM »OK, I'll give it a try. Can't promise anything, because I currently have no idea what an enum is, let alone how it works or how to properly implement it. With that said, lack of prior knowledge hasn't exactly stopped me so far
The syntax will of course vary by language, but I think an example will be the easiest way to show it:
Note that this example isn't Delphi, as it's not a language I've worked with. This is more about the concept of enums in general, not how to write them in your language of choice.
Code: [Select]
enum Colors
{
Red,
Orange,
Yellow,
Green,
Blue,
Purple
}
Basically we've just created a new type* that can only hold these values**. Internally, they're probably just ints where Red = 0, Orange = 1... Purple = 5, but code that wants a Color doesn't need to care about that. When saving/loading from a file, we can convert them to/from strings as needed; some languages offer features to do this automatically, but if not, well, if we had to do some if/else to read/write anyway without the enums, we can do it with enums too. Many languages allow you to explicitly choose an underlying type and assign your own values, but there will usually be sensible defaults.
*Well, symbolically anyway. Languages vary in whether their type system considers enums to be different from their underlying types.
**Among languages that consider it to be a separate type, they also vary in whether or not you can force it to convert a value from the underlying type to the enum type even if that value isn't part of the enum, but fortunately in many (most?) languages it's difficult to do this accidentally.
---
Well, there's also the somewhat more complex Rust enums which basically merge unions and enums into one feature, but let's not worry about those.