Which is considered as a better style? Enums, seal...
# announcements
a
Which is considered as a better style? Enums, sealed classes with singletons, or class with companion objects with val?
👍 1
n
For what?
Enums are pretty limited so I would think if you can use enums for something there's a decent chance it's the right choice
a
Enums are limited? I don't think so, they behave the same as the other two options.
n
?
They are more limited because there is less they can express
Rule of least power
a
You can have variables and functions in enums, I think you're confused with enums in low level langs 😅
n
Dude no...
Enum class instance can only be one of several globals of the exact same type
That's all it can do
Sealed classes do much more than that
a
Yes, but sealed classes with singletons (objects) are the same....
n
Yes, that's the point
Sealed classes can reasonably emulate enum classes
Enum classes cannot reasonably emulate sealed classes
Do you understand?
It's very common in programming languages that feature A can emulate B but B cannot emulate A. E.g. macros vs functions in lisp
a
The question wasn't about this, I wanted to store group of constant values. And thats the quesiton: Enums, sealed classes with singletons, or class with companion objects with val?
n
yes, and I answered your question
All other things being equal pick the least powerful language feature that serves the purpose
👍 3
This is assuming there is some notion of mutual exclusivity in your constants
Otherwise I would say none of the above, just have a bunch of top level globals in the same file