3 ms·
Modern enums in languages with powerful compilers are one of my favourite features and I repeatedly find them to be a marker of high quality codebases. In Pyth
by danpalmer 5y ago
Modern enums in languages with powerful compilers are one of my favourite features and I repeatedly find them to be a marker of high quality codebases.
In Python there has historically been a strong tendency to use strings as enums, which combined with Python's generally weak typing, can lead to all sorts of issues.
I also write a fair bit of Swift at the moment, and in Swift codebases I've found that plenty of use of enums, particularly those with attached values, leads to easy to use and correct code, as well as quite concise code. It's much better than codebases that go hard on classes.
- jamil7 5y agoYeah, I use them all the time in Swift to model UI state, I find it's a pretty elegant way to avoid invalid states and simplifies UI code a lot.
- danpalmer 5y agoYeah they're wonderful for that. Very much like Elm (and/or Haskell). Our whole iOS architecture is inspired by the Elm architecture – it's a pleasure to work with, and Swift the language makes it feel really at home.
- jamil7 5y agoWe're doing something very similar on macOS and iOS. The only rough edge I'm finding with this setup is integrating with Core Data feels a bit clunky/verbose.
- danpalmer 5y agoAh yeah I could imagine this. I haven't used CoreData much, but my understanding is that most of the framework support for it encourages bindings in a traditional MVC sort of approach. I know there is some SwiftUI support but it looks like you have to hand over a lot of control.
- amarshall 5y agoPython enums with static type-checking via Mypy are pretty pleasant, though.
- nine_zeros 5y agoType annotations are the biggest code smell. It's like we want types but only make it super complicated.
- omegalulw 5y ago> Modern enums in languages with powerful compilers are one of my favourite features and I repeatedly find them to be a marker of high quality codebases. Mega +1. I LOLed when I saw the post. My favorite feature of enums is that you limit the set of values - and thus get a lot of free checks. Not to mention being able to write case statements without default - which catches future bugs (C++).
- selfhoster11 5y agoCould you explain what you found so funny? IMO, there are real benefits to "smart" enums, especially with respect to reverse lookups.