4 ms·
This is about time, and I like that they can have functions. No stupid enum_too_to_string($bar) calls, just $bar->toString(). When I built a small, internal to
by archi42 6y ago
This is about time, and I like that they can have functions. No stupid enum_too_to_string($bar) calls, just $bar->toString().
When I built a small, internal tool a few years ago, I couldn't believe it still didn't offer something as basic as an enum type.
Didn't bother me when I learned PHP4 as a kid, but studying CS and becoming a professional developer really changes what one expects from a language.
- bob_page 6y agoThis is how I do it, currently: interface Pokemon { public const MissingNo = 0; public const Bulbasaur = 1; /* ... */ public const Slowking = 199; public const Misdreavus = 200; }
- Jach 6y agoI think at some point you turn around and become relatively unbothered again. Or you have to if you're forced to work in languages lacking things you like from other languages and don't want to feel frustrated all the time. (Doing Java and JS for years I was able to reduce the frustration to only some of the time.) Until Python 3.4, there were no enums blessed in the reference implementation, but that was fine for me. Is https://docs.python.org/3/library/enum.html https://docs.python.org/3/library/enum.html really that much better than the examples in https://web.archive.org/web/20120207145701/https://norvig.com/python-iaq.html https://web.archive.org/web/20120207145701/https://norvig.co... ? More powerful, sure. But if we just wanted simple C++ enums and not structured Java-like enums it's hard to argue against the conciseness of RED,GREEN,BLUE = range(3) and moving on with what matters. I guess two advantages of having something blessed by the language are 1) you don't need to think anymore or argue about whose workarounds (if any) are better, like the sibling's comment using PHP interfaces with consts inside, and 2) if the language isn't flexible enough to add the thing yourself, if the thing needs privileged status by the compiler or whatever, then you need the blessing for the best experience. My favorite language (Lisp) has no enums, but it's flexible enough to add them (and people have done so) in a way pretty indistinguishable from if they were there from the start, or of course you can use moral equivalents to the Python 2 examples without fuss. (Rather than just variables though there are also keywords or just plain symbols, which you can have type checked with deftype, and dispatch on with object methods if you like structured enums.)
- an_opabinia 6y agoBut people do want to reinvent Java in all their programming languages.
- atsjie 6y agoInteresting. I had the opposite reaction towards using classes for this. Mainly because of a fear of misuse. The more options a developer has the less likely he or she chooses the best solution to solve a problem. (Often best is in the eye of the beholder too, complicating things further)