Does it still make sense to use `enum` in Kotlin :...
# announcements
a
Does it still make sense to use
enum
in Kotlin 1️⃣, or do you just use
sealed class
for everything 2️⃣? Is there a disadvantage from using sealed classes?
1️⃣ 10
i
what is your purpose to use it for?
m
If enum is enough (e.g. you don't need state), you should use enum
a
what is your purpose to use it for?
my question is essentialy about when to use them, so no specific purpose
If enum is enough (e.g. you don’t need state), you should use enum
I see, although I could still use sealed class with only objects; are you suggesting this because it’s more lightweight and idiomatic? My thoughts right now is if sealed classes are a more flexible version of enum, might as well use it every time so I don’t have to refactor it.
i
s
sealed classes are meant to solve a different problem, generally
👆 3
like Milan said, if you don't need state, you should probably just use an enum
a
Thanks for your responses 🙂
s
at my company i am in charge of the kotlin code style and there i say: • use enums for the basic case (when you dont need multiple instances of one type and you dont need the types to contain logic) • favour extension functions/properties over member ones on enums, unless this would have a serious performance impact • enums should never contain business logic • use sealed classes to implement the strategy pattern • favour member functions/properties over extension ones on sealed classes this is of course subjective, but i think this way one can draw a clear line between the two, eliminating the grey area in between 🙂
b
👍