Reputation: 1582
File can be found here - https://github.com/JetBrains/kotlin/blob/master/core/builtins/native/kotlin/String.kt
Adding String.kt code for reference
package kotlin
/**
* The `String` class represents character strings. All string literals in
* Kotlin programs, such as `"abc"`, are
* implemented as instances of this class.
*/
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
/**
* Returns the character of this string at the specified [index].
*
* If the [index] is out of bounds of this string, throws an [IndexOutOfBoundsException] except in Kotlin/JS
* where the behavior is unspecified.
*/
public override fun get(index: Int): Char
public override fun subSequence(startIndex: Int, endIndex: Int): CharSequence
public override fun compareTo(other: String): Int
}
I want to understand how the methods shown doesn't have any implementation? Also please brief on how this code could possibly work?
If there is native code being used. Please link the source code of the same..
Upvotes: 2
Views: 1046
Reputation: 2463
Kotlin's standard library is shared between all the platforms supported by Kotlin, so it contains only signatures of methods. Several classes are implemented identically to platform-specific ones: for example, in Kotlin/JVM kotlin.text.StringBuilder
is implemented as a type-alias to java.lang.StringBuilder
.
However, some types are mapped by the compiler specifically to platform types (some methods are removed, some methods are renamed). These types include primitive types (Int, Byte, etc.), String and collections.
As for JVM the rough mapping is:
kotlin.String.length => java.lang.String.length()
kotlin.String.compareTo(other: String) => java.lang.String.compareTo(java.langString anotherString)
kotlin.String.get(index: Int) => java.lang.String.charAt(int index)
kotlin.String.plus(other: Any?) is implemented as plain Java concatentation (it is implemented on compiler level with StringConcatFactory magic)
kotlin.String.subSequence(startIndex: Int, endIndex: Int) => java.lang.String.subSequence(int beginIndex, int endIndex)
Other java.lang.String
methods like intern
are not available in kotlin.String
(however, it is actually possible to reference java.lang.String
directly).
In Kotlin/Native kotlin.String
is implemented with C++: https://github.com/JetBrains/kotlin-native/blob/045b20a36b8a1fe86716a99cb25f03021e583592/runtime/src/main/cpp/KString.cpp.
JavaScript implementation involves builtin JavaScript String too.
Upvotes: 3