为什么某些 Kotlin 库函数(例如 String.kt 中的一些扩展函数)标记为内联,即使它们不是高阶函数?
Why are certain Kotlin library functions (e.g., some extension functions in String.kt) marked as inline even if they are no higher-order functions?
The Kotlin documentation 本身声明如下:
If an inline function has no inlinable function parameters and no reified type parameters, the compiler will issue a warning, since inlining such functions is very unlikely to be beneficial.
没有内联函数参数和没有具体化的类型参数这两个语句都适用,例如,以下扩展方法String.kt
:
public inline fun String.reversed(): String
public inline fun String.slice(indices: Iterable<Int>) : String
public inline fun CharSequence.random(): Char
任何人都可以向我解释语言设计者可能决定将这些方法标记为内联的具体原因吗?谢谢。
正如@somethingsomething 在评论中指出的那样,这个问题已经在 之前得到了回答。
那里的答案(来自 JetBrains 员工)是:
This particular function and a few others in kotlin-stdlib are marked as @InlineOnly
so that they are not present in the actual stdlib class files and are only available for the Kotlin compiler to inline them. The goal that is achieved in this way is reducing the methods count in the artifacts, which matters for Android.
还讨论了 @InlineOnly
注释 。
The Kotlin documentation 本身声明如下:
If an inline function has no inlinable function parameters and no reified type parameters, the compiler will issue a warning, since inlining such functions is very unlikely to be beneficial.
没有内联函数参数和没有具体化的类型参数这两个语句都适用,例如,以下扩展方法String.kt
:
public inline fun String.reversed(): String
public inline fun String.slice(indices: Iterable<Int>) : String
public inline fun CharSequence.random(): Char
任何人都可以向我解释语言设计者可能决定将这些方法标记为内联的具体原因吗?谢谢。
正如@somethingsomething 在评论中指出的那样,这个问题已经在
那里的答案(来自 JetBrains 员工)是:
This particular function and a few others in kotlin-stdlib are marked as
@InlineOnly
so that they are not present in the actual stdlib class files and are only available for the Kotlin compiler to inline them. The goal that is achieved in this way is reducing the methods count in the artifacts, which matters for Android.
还讨论了 @InlineOnly
注释