java - Android 的 Kotlin "expensive"中的枚举类是否与 Java 中的一样?

标签 java android kotlin enums

  • 对于 Android,Kotlin 中的Enum 类是否像 Java 一样“昂贵”?
  • 在 Kotlin 中使用 @IntDefs@StringDefs 是否仍然有效?

当我将 Kotlin Enum 类反编译为 Java 类时,底层仍然使用了 Java Enums,不推荐在 Android 开发中使用,这让我想到了这一点。

最佳答案

  1. 对于 Android,Kotlin 中的 Enum 类是否像 Java 一样“昂贵”?可能是。我怀疑 kotlin 是否能够进行 java 无法进行的任何优化。
  2. 在 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/

相关文章:

Android 商店用户购买的媒体文件,因此只有应用程序可以访问

android - 嵌套 ScrollView 在 CoordinatorLayout 中占用额外空间

kotlin - 无法使用 Room ORM Kotlin 保存包含 List<Model> 的数据模型

java - 如何获得在数组中写入不同或缺失元素的输出?

java - 使用box java sdk下载项目而不知道它是文件还是文件夹

java - 如何删除FTP目录中的所有txt文件?

Android Studio,构建 kotlin : wrong code generated 时出现奇怪错误

intellij-idea - 在 kotlin 中为 gradle 脚本配置 IntelliJ 自动完成

java - 为什么 2010 年 12 月 31 日返回 1 作为一年中的一周?

java - com.vividsolutions.jts.geom.Geometry 类中的距离单位是什么?