3

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.

Markus Weninger
  • 11,931
  • 7
  • 64
  • 137
  • I think it might be to prevent Java code from calling them, since all the examples you showed are also marked `InlineOnly`. But I don't see why Java code calling them would be bad... – Sweeper Nov 17 '21 at 08:53
  • 1
    Seems like a clone of https://stackoverflow.com/questions/46830932/why-use-inline-without-lambdas, the marked answer is by a jetbrains employee – somethingsomething Nov 17 '21 at 09:22

1 Answers1

0

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.

Markus Weninger
  • 11,931
  • 7
  • 64
  • 137