- Are Enum classes in Kotlin “expensive” for Android as Java?.
- Is it still stand to use
@IntDefs
or@StringDefs
in Kotlin?.
When I decompile Kotlin Enum class to Java class, there still used Java Enums in the low level, which are not recommended for use in Android Development, and this made me think about this.
Advertisement
Answer
- Are Enum classes in Kotlin “expensive” for Android as Java?. Could be. I doubt whether kotlin will be able to do any optimization that java cannot.
- Is it still stand to use @IntDefs or @StringDefs in Kotlin?. – Not really. This is a polarising subject and many in the Android world(including me) believe that using enums should be the preferred way. I encourage you to read the following links to get a better idea.
Twitter thread by Jake Wharton
An article by Diane Hackborn and the comments
Somwhat witty reddit thread talking about optimization of enums and such
And the following answers by Jake Wharton are interesting.
Does lint support kotlin typedefs? (Lint for Kotlin won’t be fully supported until AS 3.1, and we’re on 3.0 still)
Do you mean typealias? If so, yes.
Will I get a lint warning for an exhaustive when when I do a return when(intDef) { .. } and accidentally omit one of the the possible types?
An else will always be required here. Not sure about the lint warning though.
Does AS have an intention (alt + enter to quickly add all variants of the intDef?
Yes.
Like right now, instead of shipping features, we’re discussing when this micro-optimization should be used!
Summation of the entire enum conversation since about 2012!
I’m also wondering if a sealed class where each implementation is an object has some perf gains over an enum
None. It’s effectively the exact same thing as an enum.