JavaScript to Kotlin Conversion
The article lists the available types in JavaScript and how they are projected to Kotlin.
String
JavaScript String maps to kotlin.String:
var kotlinClass = new com.example.KotlinClassWithStringProperty();
var text = "My Button"; // JavaScript string
kotlinClass.setStringProperty(text); // text is converted to kotlin.String
Boolean
JavaScript Boolean maps to Kotlin class Boolean.
var kotlinClass = new com.example.KotlinClassWithBooleanProperty();
var enabled = false; // JavaScript Boolean
kotlinClass.setBooleanProperty(enabled); // enabled is converted to Kotlin Boolean
Undefined & Null
JavaScript Undefined & Null maps to Kotlin null literal (or null pointer).
var kotlinClass = new com.example.KotlinClassWithNullableParameter(undefined); // the Kotlin call will be made using the null keyword
Number
Kotlin has several numeric types while JavaScript has the Number type only. Additionally, unlike JavaScript, Kotlin is a language that supports Method Overloading, which makes the numeric conversion more complex. Consider the following Java class:
class MyObject : Any() {
fun myMethod(value: Byte) {}
fun myMethod(value: Short) {}
fun myMethod(value: Int) {}
fun myMethod(value: Long) {}
fun myMethod(value: Float) {}
fun myMethod(value: Double) {}
}
The following logic applies when calling
myMethod
on a myObject
instance from
JavaScript:
var myObject = new MyObject();
- Implicit integer conversion:
myObject.myMethod(10); // myMethod(Int) will be called.
Note: If there is no myMethod(Int) implementation, the Runtime will try to choose the best possible overload with least conversion loss. If no such method is found an exception will be raised.
- Implicit floating-point conversion:
myObject.myMethod(10.5); // myMethod(Double) will be called.
Note: If there is no myMethod(Double) implementation, the Runtime will try to choose the best possible overload with least conversion loss. If no such method is found an exception will be raised.
-
Explicitly call an overload:
To enable developers call a specific method overload, the Runtime exposes the following functions directly in the global context:- byte(number) → Kotlin Byte
The number value will be truncated and only its first byte of the whole part will be used.
- short(number) → Kotlin Short
The number value will be truncated and only its first 2 bytes of the whole part will be used.
- float(number) → Kotlin Float
The number value will be converted (with a possible precision loss) to a 2^32 floating-point value.
- long(number) → Kotlin Long (in case the number literal fits JavaScript 2^53 limit)
The number value's whole part will be taken only.
- long("number") → Kotlin Long (in case the number literal doesn't fit JavaScript 2^53 limit)
myObject.myMethod(byte(10)); // will call myMethod(Byte)
myObject.myMethod(short(10)); // will call myMethod(Short)
myObject.myMethod(float(10)); // will call myMethod(Float)
myObject.myMethod(long(10)); // will call myMethod(Long)
myObject.myMethod(long("123456")); // will convert "123456" to Kotlin Long and will call myMethod(Long)
Note: When an explicit cast function is called and there is no such implementation found, the Runtime will directly fail, without trying to find a matching overload.
Array
A JavaScript Array is implicitly converted to a Kotlin Array, using the above described rules for type conversion of the array's elements. For example:
class MyObject : Any() {
fun myMethod(items: Array<String>) {}
}
var items = ["One", "Two", "Three"];
var myObject = new MyObject();
myObject.myMethod(items); // will convert to Java array of java.lang.String objects