I have known that Kotlin was awesome since I switched to it from java, 2 years ago.
But recently, I have tried to use it for javascript target and I have been blown away by Kotlin…again.
It is nice, simple and it works. I’m really impressed (I have been able to write a non trivial piece of javascript code really fast).
- Please document the javascript specific features !
the documentation for Kotlin:JS is lacking a lot : before you can setup a working project and understand how to make it work, you have to google search a lot of old blog posts and guess a lot. But after you get figured out the following stuff, it is pure awesomeness.
- Calling a top level Kotlin function from javascript
// in Kotlin
yourPackage.yourFunction()// in javascript
Kotlin.modules.yourLibrary.yourPackage.yourFunction();
- using the @Native annotation to use a javascript instance/object/function in Kotlin
// accessing a javascript class
// in there, the javascript qualified type
@native(“math.expression.node.Node”)
// the corresponding javascript constructor (with the right parameters)
open class Node() {
// corresponding signature of the javascript methods you want to use
fun toTex():String
fun transform(action:(node:Node, path:String, parent:Node?)-> Node):Node
fun clone():Node
}// accessing a subclass of Node
@native(“math.expression.node.SymbolNode”)
class SymbolNode(val name: String):Node()// using an object
@native(“math”)
object math {
fun parse(string:String):Node
}// using a val (can’t add method signatures)
@native(“math”)
val math = noImpl// creating a javascript class in Kotlin
val node = SymbolNode(“Hello”)// accessing it’s value in Kotlin (the output goes in the javascript console)
println(node.name)// accessing it’s value (the output goes in the html document)
document.writeln(node.name)// using a method of a javascript object
val node2 = math.parse(“it really works !”)
-
dynamic (working with javascript objects)
-
js() (calling javascript from kotlin only with constant strings…)
-
jsTypeOf()
-
Is there a way to use @Native class instances as keys in HashMap ?
How does hashCode and equal work ? is it possible to overload them ?
At the moment, I have to use instance.toString() as a key -
is there a list of features available for the javascript target ? A roadmap ?