Kotlin to JavaScript Conversion
The article lists the available types in Kotlin and how they are projected to JavaScript.
Keep in mind that some of Kotlin's fundamental types are translated to a Java type by the Kotlin compiler when targeting Android or the JVM. Those types are the following:
Kotlin non-nullable type | Java type | Kotlin nullable type | Java type |
---|---|---|---|
kotlin.Any | java.lang.Object | kotlin.Any? | java.lang.Object |
kotlin.String | java.lang.String | kotlin.String? | java.lang.String |
kotlin.Char | char | kotlin.Char? | java.lang.Character |
kotlin.Boolean | boolean | kotlin.Boolean? | java.lang.Boolean |
kotlin.Byte | byte | kotlin.Byte? | java.lang.Byte |
kotlin.Short | short | kotlin.Short? | java.lang.Short |
kotlin.Int | int | kotlin.Int? | java.lang.Integer |
kotlin.Long | long | kotlin.Long? | java.lang.Long |
kotlin.Float | float | kotlin.Float? | java.lang.Float |
Although the conversion of Kotlin types in NativeScript is quite the same as the Java conversion, let's take a look at some examples.
String & Character
Both kotlin.String and kotlin.Char types are projected as JavaScript String:
Boolean
Kotlin's boolean type kotlin.Boolean is projected as JavaScript Boolean:
Byte
Kotlin's byte type kotlin.Byte is projected as JavaScript Number:
Short
Kotlin's short type kotlin.Short is projected as JavaScript Number:
Integer
Kotlin's integer type kotlin.Int is projected as JavaScript Number:
Float
Kotlin's float type kotlin.Float is projected as JavaScript Number:
Double
Kotlin's double type kotlin.Double is projected as JavaScript Number:
Long
Kotlin's long type kotlin.Long is a special type which is projected to JavaScript by applying the following rules:
- If the value is in the interval (-2^53, 2^53) then it is converted to Number
- Else a special object with the following characteristics is created:
- Has Number.NaN set as a prototype
- Has value property set to the string representation of the Kotlin long value
- Its valueOf() method returns NaN
- Its toString() method returns the string representation of the Kotlin long value
Array
Array in Kotlin is a special object that has an implicit Class associated. A Kotlin Array is projected to JavaScript as a special JavaScript proxy object with the following characteristics:
- Has length property
- Has registered indexed getter and setter callbacks, which:
- If the array contains elements of type convertible to a JavaScript type, then accessing the n-th element will return a converted type
- If the array contains elements of type non-convertible to JavaScript, then accessing the n-th element will return a proxy object over the Kotlin type (see Accessing APIs)
Note: A Kotlin Array is intentionally not converted to a JavaScript Array for the sake of performance, especially when it comes to large arrays.
Creating arrays
Occasionally you have to create Kotlin arrays from JavaScript. Because of the translation of the fundamental Kotlin types to Java types in Android, creating Kotlin array could be done the same way Java arrays are created. This is described in Java to JavaScript
Null
The Kotlin null literal (or null pointer) is projected to JavaScript Null:
Kotlin Types
All Kotlin types are projected to JavaScript using the Package and Class proxies as described in Accessing APIs
Kotlin Companion objects
Kotlin's companion objects could be accessed in JavaScript the same way they can be accessed in Java - by accessing the Companion
field:
Kotlin Object
Kotlin's objects could be accessed in JavaScript the same way they can be accessed in Java - by accessing the INSTANCE field:
Accessing Kotlin properties
Kotlin's properties could be accessed in JavaScript the same way they can be accessed in Java - by using their compiler-generated get/set methods. Non-boolean Kotlin properties could be used in NativeScript applications as JS fields as well.
Accessing Kotlin package-level functions
Currently using Kotlin package-level functions could not be achieved easily. In order to use a package-level function, the class where it's defined should be known. Let's take a look at an example:
In the example above, the class FunctionsKt
is autogenerated by the Kotlin compiler and its name is based on the name of the file where the functions are defined. Kotlin supports annotating a file to have a user provided name and this simplifies using package-level functions:
Accessing Kotlin extension functions
Currently using Kotlin extension functions could not be achieved easily. In order to use an extension function, the class where it's defined should be known. Also, when invoking it, the first parameter should be an instance of the type for which the function is defined. Let's take a look at an example:
In the example above, the class ExtensionsKt
is autogenerated by the Kotlin compiler and its name is based on the name of the file where the functions are defined. Kotlin supports annotating a file to have a user provided name and this simplifies using package-level functions: