简体   繁体   English

Kotlin 中的枚举类对于 Android 来说是否像 Java 一样“昂贵”?

[英]Are Enum classes in Kotlin "expensive" for Android as in Java?

  • Are Enum classes in Kotlin "expensive" for Android as Java?. Kotlin 中的Enum类对于 Android 作为 Java 是否“昂贵”?
  • Is it still stand to use @IntDefs or @StringDefs in Kotlin?.在 Kotlin 中仍然使用@IntDefs@StringDefs吗?。

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.当我将 Kotlin Enum 类反编译为 Java 类时,仍然使用了底层的 Java Enum,不推荐在 Android 开发中使用,这让我想到了这一点。

  1. Are Enum classes in Kotlin "expensive" for Android as Java?. Kotlin 中的 Enum 类对于 Android 作为 Java 是否“昂贵”? Could be.可能。 I doubt whether kotlin will be able to do any optimization that java cannot.我怀疑 kotlin 是否能够做任何 java 不能做的优化。
  2. Is it still stand to use @IntDefs or @StringDefs in Kotlin?.在 Kotlin 中仍然使用 @IntDefs 或 @StringDefs 吗?。 - Not really. - 并不真地。 This is a polarising subject and many in the Android world(including me) believe that using enums should be the preferred way.这是一个两极分化的主题,Android 世界中的许多人(包括我)认为使用枚举应该是首选方式。 I encourage you to read the following links to get a better idea.我鼓励您阅读以下链接以获得更好的想法。

Twitter thread by Jake Wharton 杰克·沃顿 (Jake Wharton) 的推特帖子

An article by Diane Hackborn and the comments黛安·哈克伯恩 (Diane Hackborn) 的一篇文章和评论

Somwhat witty reddit thread talking about optimization of enums and such 一些诙谐的 reddit 线程谈论优化枚举等

And the following answers by Jake Wharton are interesting.杰克·沃顿 (Jake Wharton) 的以下回答很有趣。

Does lint support kotlin typedefs? lint 是否支持 kotlin 类型定义? (Lint for Kotlin won't be fully supported until AS 3.1, and we're on 3.0 still) (直到 AS 3.1 才会完全支持 Kotlin 的 Lint,我们仍然在 3.0 上)

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?当我执行 return when(intDef) { .. } 并且不小心遗漏了其中一种可能的类型时,我会收到详尽的 lint 警告吗?

An else will always be required here.这里总是需要一个 else。 Not sure about the lint warning though.虽然不确定 lint 警告。

Does AS have an intention (alt + enter to quickly add all variants of the intDef? AS 是否有意(alt + enter 以快速添加 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!自 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.它实际上与枚举完全相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM