- 对于 Android,Kotlin 中的Enum 类是否像 Java 一样“昂贵”?
- 在 Kotlin 中使用
@IntDefs
或@StringDefs
是否仍然有效?
当我将 Kotlin Enum 类反编译为 Java 类时,底层仍然使用了 Java Enums,不推荐在 Android 开发中使用,这让我想到了这一点。
最佳答案
- 对于 Android,Kotlin 中的 Enum 类是否像 Java 一样“昂贵”?可能是。我怀疑 kotlin 是否能够进行 java 无法进行的任何优化。
- 在 Kotlin 中仍然可以使用 @IntDefs 或 @StringDefs 吗? - 不是。这是一个两极分化的话题,Android 世界中的许多人(包括我)认为使用枚举应该是首选方式。我鼓励您阅读以下链接以获得更好的想法。
Twitter thread by Jake Wharton
An article by Diane Hackborn and the comments
Somwhat witty reddit thread talking about optimization of enums and such
Jake Wharton 的以下回答很有趣。
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.
关于java - Android 的 Kotlin "expensive"中的枚举类是否与 Java 中的一样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54892823/