Reputation: 83
I'm currently evaluating to port a large Java/Swing application to Kotlin. The domain of the application is 2D graphics, graphs, graphical editing and simulation/animation. I want to rewrite the entire application core in Kotlin and to transpile it to Java byte code as well as to JavaScript. I'm currently able to write Kotlin drawing code that renders both in a Java window and in a HTML canvas, using some adapter and bridge classes. So far so good.
Now I'm struggling with dependency injection, which I'm intensively using in my application. I want to inject dependencies in my Kotlin classes and still be able to transpile them to the JVM and to JavaScript, so I looked for DI frameworks for Kotlin. I found injekt and kodein, but it looks like they both rely on JDK classes, so they are not applicable for my JavaScript use case. I therefore started to develop my own pure Kotlin DI framework, which went well until I realized that the Kotlin transpiler for JavaScript doesn't yet support reflection, without which you can't do dependency injection.
Does anybody know the plans of JetBrains to support reflection in their JavaScript transpiler in the near future? Or does anybody know another way of writing pure Kotlin code that uses dependency injection, and that can still be transpiled both to Java and to JavaScript? This issue might turn out as a Kotlin show-stopper for my project, since I'm not sure whether I want to go back to static configurable factories.
Upvotes: 5
Views: 657
Reputation: 2085
Supporting reflection in Kotlin to JavaScript compiler is a tricky thing. The worst thing is that you have to somehow store metadata in JS file, which makes JS file large, and unlike JAR, which can be as large as needed, it's very important to have small JS files. Also, you have to bundle code that unpacks this metadata and exposes reflection API, which makes generated JS code even larger.
I'm not sure whether reflection will be ever implemented for JS compiler (or if it will, it likely won't be recommended way to implement things like DI). It definitely won't be available in 1.1. Instead, there might be another approach similar to GWT generators, where developer can write special compiler plugins and generate some code to support things like serialization, RPC, DI, etc. We are currently experimenting with JSON serialization, and we generate serializer during compile-time. If we get good results, we might open our compiler plugin API so that any user can implement similar things.
Also, I have a spare-time project that has a similar goal and it implements its own metaprogramming API which I use to implement large subset of Jackson serializer without any reflection, as well as JAX-RS client proxies. However, I'm still failing to push remaining Kotlin team to adopt my ideas and design something similar for Kotlin compiler.
Currently, you have nothing except using so-called "poor man's DI", since IoC is mostly design pattern, not a library. With Kotlin capabilities of creating DSL's I believe it's possible to create something nice.
Upvotes: 0