简体   繁体   中英

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 itself states the following:

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.

Both statements, no inlinable function parameters and no reified type parameters , are true for, for example, the following extension methods in String.kt :

  • public inline fun String.reversed(): String
  • public inline fun String.slice(indices: Iterable<Int>): String
  • public inline fun CharSequence.random(): Char

Can anyone explain me a specific reason why the language designers probably made the decisions to mark these methods as inline? Thanks.

As @somethingsomething pointed out in the comments, this question has been answered in a similar question before.

The answer there (by a JetBrains employee) was:

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.

The @InlineOnly annotation is also discussed in this question .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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