After several days of work, my Kotlin-angularjs demo is out there. Thanks for everyone, you have helped me a lot these days.
The demo is coming from the code from “todomvc.com”, what I have done was using Kotlin to rewrite the javascript code.
You can see it here: https://github.com/freewind/kotlin-angularjs/tree/master/src
Since I’m very new to Kotlin, I think the code can be improved, could you please review my code and help me?
And I have some questions about Kotlin-javascript compiler:
- Can we convert a class into a json like object in javascript? e.g.
class User(name:String, age: Int)
val user = User("freewind", 100)
be compiled to:
{
name: "freewind",
age: 100
}
2. Can we ignore the parameter name when we declare a "native" function?
native fun <T> Array<T>.push(x: T) = js.noImpl
native fun <T> Array<T>.splice(i1: Int, i2: Int) = js.noImpl
native fun <T> Array<T>.indexOf(x: T) = js.noImpl
native fun <T> Array<T>.filter(x: (T)->Boolean) = js.noImpl
native fun <T> Array<T>.forEach(x: (T)->Unit) = js.noImpl
native val <T> Array<T>.length: Int = js.noImpl
The “x” are unused, but I have to declare them.
- Can we make the function literal simpler?
{ (name:String, age: Int) -> "do some thing" }
Can we write it as:
{ name:String, age: Int -> "do some thing" }
or
(name:String, age:Int) -> "do some thing"
4. Can we specify the native name of a parameter?
{ ( native("$scope") scope: Scope, native("$timeout") timeout: Timeout) -> ... }
Which doesn't work
- How to declare a function literal with “vararg” ?
native fun directive(name:String, def: (vararg Any) -> Any) = noImpl
But when I use it as:
directive("myfocus", { (scope:Any, timeout:Any) -> "" })
It can't be compiled, and the error is: Expect one parameter of type jet.Any
- about “$”
native("$timeout") val timeout:Timeout = noImpl
We have to write "$", is it about to just to write "$" with some specify syntax in the future?
return JSON.parse<String>(data!!) as Array<Todo>
Warning: This cast can never succeed. How to fix it?
directive.link = { scope, elem, _attrs ->
val attrs = _attrs as Attrs
Can we just declare the parameters as "scope, elem, attrs:Boolean" in the future?
- remove noImpl
native var angular:Angular = noImpl
You can see there is a "noImpl" placeholder, which is not useful. Can we remove it in the future?
So we just write:
native var angular:Angular
10. Can we declare an array with type (any String parameters, with a function in the end)
angularjs.controller("Todo", ["$scope", "$timeout", function(scope, timeout) {} ])
Is it possible to declared such an type for the second parameter?
(vararg names:String, func: (vararg String) -> Any)