6

In kotlin source code, I can't understand how to implement of length of String.kt , it is as following:

package kotlin                                                  
public class String : Comparable<String>, CharSequence {
companion object {}

/**
 * Returns a string obtained by concatenating this string with the string representation of the given [other] object.
 */
public operator fun plus(other: Any?): String

public override val length: Int

public override fun get(index: Int): Char

public override fun subSequence(startIndex: Int, endIndex: Int): CharSequence

public override fun compareTo(other: String): Int}

var len:Int = "abc".length; // len = 3 where to run the length??

where to implement the length function?

Jim Green
  • 1,088
  • 3
  • 15
  • 40

1 Answers1

13

The string functions are examples of what Kotlin considers Intrinsic functions. They are defined based on the platform they are running on and you won't be able to find an implementation of them in the source code.

For the JVM they will be directly mapped to the corresponding native java.lang.String methods. This ensures there is no runtime overhead and leverages the optimizations done in the java standard library.

Kiskae
  • 24,655
  • 2
  • 77
  • 74
  • There are no 'implementations', when those functions are called the compiler does some custom code generation to produce the result. Some of the mapped functions can be found at https://github.com/JetBrains/kotlin/blob/master/compiler/backend/src/org/jetbrains/kotlin/codegen/intrinsics/IntrinsicMethods.java – Kiskae Jun 15 '17 at 13:49