由于属性gettersetter通常既没有作为参数的函数,也没有reified类型,那么使用inline属性的好处/用例是什么?

If the benefits would be to reduce cost related to method call, why not make all properties getter/setter inline by default?

Kotlin Inline Properties

如.

val foo: Foo
    inline get() = Foo()

var bar: Bar
    get() = ...
    inline set(v) { ... }

推荐答案

Here is inline properties discussion:

Example of reified type parameter:

inline val <reified T : PsiElement> T.nextSiblingOfSameType: T?
    get() = PsiTreeUtil.getNextSiblingOfType(this, T::class.java)

Another use case is to hide some properties from the binary interface of a library. In Kotlin standard library, together with the @InlineOnly annotation this might make it possible in the future to exclude declarations of such properties from class files, reducing method count, this will mostly benefit Android with it's 64K method limit.

Kotlin相关问答推荐

如果一项工作失败,请继续在Kotlin 等待其他工作/子元素完成

Kotlin和JavaFX:绑定行为奇怪

Kotlin协程挂起继续线程

Kotlin 海峡没有结束

如何在Spring Boot中注册新的集合工厂

kotlin短路列表判断吗?

Mockk:对同一函数进行两次存根会忽略第一个行为

Kotlin 函数中接收者和参数的类型相同

返回 kotlin 中的标签和 lambda 表达式

Saripaar formvalidation 在 kotlin 中第二次不起作用

Swift vs Kotlin 在排序数组上的表现

使用 clear() 删除 EncryptedSharedPreferences 不起作用

kotlin-bom 库是做什么的?

用mockk验证属性设置程序吗?

如何在 Jetpack Compose 的 LazyColumn/LazyRow 中禁用和启用滚动?

如何在使用Koin DI的活动之间共享同一个ViewModel实例?

可以在函数参数中使用解构吗?

在 kotlin 中,如何将主构造函数中的属性设置器设为私有?

带有注释为@RegisterExtension的字段的 JUnit 5 测试在 Kotlin 中不起作用

如何将 Kotlin 的 `with` 表达式用于可空类型