Transcoding to other languages via the sytax tree

Technically speaking, you could get the abstract syntax tree and just translate it into another language to make it interop(and also the other way around). With that, you could use Kotlin in any existing codebase and could therefore make it the language for everything. Features that do not exist could simply be recreated, and the translation would just be stored as invisible files.

That sounds similar to your another topic. Yes, this is technically possible and I believe this is more or less what the Kotlin compiler does right now, especially the new K2 compiler. Being more precise, I believe we don’t translate AST from one language to another directly, but rather introduce an IR (intermediate representation) that is language-agnostic. This is what LLVM does and probably the K2 compiler as well.

No, it wouldn’t be enough: you also have to rebuild the entire standard library (or at the very least all compiler intrinsics) for that language as well.

For example, Kotlin has Long which is a 64-bit integer type. JS doesn’t have one, so the Kotlin team had to implement their own.

1 Like

I did this, and I strongly regret it.

If you’d like to mess with it, you go right ahead, but I’m warning you, maintaining a monster like this is no joke. I was never able to put enough time into it to make it a good user experience.

Honestly, if I could maintain this giant, there are things about it that are significantly nicer than current multiplatform. Its interop is extremely efficient, since you can straight-up drop down into the target language for anything you need. The JS output is tiny. However, in order to make it possible, it sacrificed a lot on perfect compatibility. There are all sorts of strange edges you can create issues with, like with numbers as clovisai mentioned. You have to program Kotlin as if you were living with the Swift memory model which is awkward. You can’t use generics on interfaces due to Swift protocols.

The JetBrains team has taken the correct approach and has the manpower to maintain it. It hurt my pride to leave it behind and start moving everything back to Multiplatform, but it’s the right call.

3 Likes