Kotlin as tool for plain javascript generation


#1

Plain mean not depend from jvm classes and kotlinLib.js, only build-in javascript api and libraries like jQuery.

When it can be useful

  1. I’m already have some js files working on my site, so I don’t wont rewrite them. All I want it’s just generate exactly the same files by using Kotlin.
  2. I’m working at company, and they don’t want to use Kotlin, so js files can look like I write them by myself.
  3. I think It can be simpler write some js to Kotlin convertor.
  4. May be use it for generate some core/low level Kotlin js files.

All we need it’s ability to generate any javascript constructions. But these are several problems
1)Square brackets

[SomeAnnotation]
fun get(index:Int):T = js.noImpl
list[0]

generate list[0] instead of list.get(0)
2) Access to this

fun test([ThisParam] x:A){
  x.foo;
}

generate
function test(){
  this.foo
}
instead of
function test(x){
  x.foo
}
3) Simplify classes

[JsSimple]
class A(x:Int){
  val foo = x
}

generate something like

function A(x){
  this.foo = x;
}

etc

What do you think?


#2

As far as I understand you want to translate Kotlin to human readable and as simple as possible JavaScript.

I would say it is possible to some extent but IMO there are a couple of cons to this approach:

  1. Human readability is possible for simple language constructs. If you look at JavaScript generated from CoffeeScript the code will be simple and readable but still you will rarely mistake it for hand-written JavaScript. With more complex features of Kotlin like multiple inheritance, extension literals, closures, etc maintaining even that level of clarity can be a challenging problem.
  2. Kotlin is statically typed, has some nice features and kool… But I don’t see how having another backend can be useful if you don’t aim to share the standard library between the backends and have the possibility to share code that compiles to different targets.


Thanks for feedback.


#3

Main problem is migration from js to Kotlin. I think Kotlin will be more popular if this process can be as simple as posible. Is there any plans to create  some sort of javascript to Kotlin convertor? 1) I really can live without complex features, just want all javascript.already have. 2) I think many people can use Kotlin as javascript alternative without care about another backends


#4

It's not an easy task to produce typed code from JavaScript. Personally I think that even if JavaScript to Kotlin converter was there, it would give adequate output on very small subset of cases. If you want to try something like that, feel free to ask any questions.

IMO Better thing to do is to provide an ability to interop with existing code. There ase some ways to do that now (js.native annotation), they may change in future versions.

  1. If you're satisfied with features that JavaScript has at the moment, then why do you need Kotlin at all? Static typing and IDE support?
  2. Good point.

#5

Yes, but when Kotlin will start support dynamic type, is't start to be unnecessary produce typed code, converter can use Dynamic everywhere. It's hard to decide what better use two languages or just one bad. 1 Exactly