Hello everyone, I am learning Kotlin and I have a ...
# android
c
Hello everyone, I am learning Kotlin and I have a question about enum vs sealed classes. Should I use sealed when my enum constants needs diffferent constructors ? Should I always use sealed classes no matter what ...
👋 1
s
Yeah, sealed classes work well when you have different constructors for your enums (they're different types, really). 👍
I still use enums for simpler things, but I'm not sure if there's a performance overhead for using sealed classes all the time.
c
😄Thank you for your feedback. For the overhead, I have no idea I did not read the generated bytecode
s
If you can use an enum for your situation, then I guess it's one of the “do whatever makes you feel good” situations. 😎
👍 1
e
I can imagine, but I'm not sure about this one, that bytecode optimisation (R8) can perform better with enums than with sealed classes. Use enums when possible, but sealed classes if what you need can't be done with enums
d
On other hand - do you really need to care about so low optimization factors? Only few kind of apps should be aware of that. Most of the apps from the code perspective should focus on code readibility in first place.
👍 3
j
You get an ordinal using regular enums but sealed classes are also nice together with when.
e
Enums go well with
when
too!
j
Exactly