https://kotlinlang.org logo
#javascript
Title
# javascript
e

Edoardo Luppi

03/14/2024, 9:20 PM
I was thinking, what about training an AI model to translate TypeScript declarations to Kotlin externals? Doesn't sound like a totally crazy idea.
👀 1
e

eygraber

03/14/2024, 10:09 PM
Isn't that just a transpiler?
e

Edoardo Luppi

03/14/2024, 10:57 PM
Well, sometimes you can't transpile 1 to 1, there are places where the Kotlin code must be adapted to make it bareable/working. And writing a perfectly working transpiler takes a lot of time. Especially considering TS evolves very fast.
e

eygraber

03/15/2024, 2:36 AM
I haven't checked in a while, but I guess dukat still doesn't always work?
h

hfhbd

03/15/2024, 5:18 AM
A translator should always return the same transpiled program for a given input, especially when the transpiled code isn’t checked manually but part of your generated sources executed each build. If you want to use an AI model, you should make it reproducible, which is currently not the case of the current models, eg ChatGPT returns different results for the same input prompt.
a

Arjan van Wieringen

03/15/2024, 9:26 AM
Not everything is a nail
e

Edoardo Luppi

03/15/2024, 10:27 AM
part of your generated sources executed each build
That would be out of scope. Having a transpiler work that reliable takes years. I currently translate d.ts files using Karakum since Dukat is way outdated, and the result is always something that doesn't compile. Getting the generation to compile requires investing weeks into Karakum plugins, which are still undocumented and using untyped JS.
If an AI can give me a decent starting point, at least I don't have to lose too much time fixing code myself.
r

Reuben Firmin

03/15/2024, 11:06 AM
I agree with this - it would be a great AI project for JetBrains to take on. Could be a simple webapp that they host which looks at a github repo or npm listing and dumps out a kotlinjs binding that gets you started. It's probably the biggest unsolved pain point in using KotlinJs
a

Arjan van Wieringen

03/15/2024, 6:22 PM
The problem with Dukat and transpiling from Typescript is that the expressivity in Typescript is waaaaaay larger then Kotlin can support. Think about how crazy you can get with Union types for instance. Or Omit and Pick types. I remember a KotlinConf 19 talk about thisb
e

Edoardo Luppi

03/15/2024, 6:24 PM
That's why you need a model that can be trained over time. Doing it manually or via a manually coded tool will always be painful
a

Arjan van Wieringen

03/15/2024, 6:24 PM
No that isn’t the problem. The problem is that there is no way you can fit the Typescript types on the Kotlin language. It works for the most basic types yes. But that’s it
There are things like Dependent Types. Or type literals. Or union types. The union types can cause your Kotlin generation to blow up
e

Edoardo Luppi

03/15/2024, 6:26 PM
Types like Omit/Pick, or unions, can definitely be reproduced in Kotlin. But it takes a human to do it. I know because I had to create VS Code externals and there is a bunch of that stuff.
a

Arjan van Wieringen

03/15/2024, 6:27 PM
I didn’t say you can’t, but you cant do it reliably. An AI model helps for the grunt work but there are a lot of types that just don’t map to any Kotlin equivalent
e

Edoardo Luppi

03/15/2024, 6:29 PM
If you can train the model on existing "mappings", it may be able to output decent code. Having it compile immediately is a non goal, having it output decent code that doesn't require days to fix is the goal.
a

Arjan van Wieringen

03/15/2024, 6:29 PM
That isn’t the problem. The problem is that the type system of Typescript is orders of magnitude more expressive that Kotlins. You just can’t map it for all but the most easy ones.
e

Edoardo Luppi

03/15/2024, 6:31 PM
And I know it, but as I wrote you can circumvent the limitations in various ways. The same ways an AI model could be trained on. Overloads, interface hierarchies, extension functions, etc.
a

Arjan van Wieringen

03/15/2024, 6:35 PM
Well, feel free to try ;) I love being proven wrong especially for something so useful. However I tend to lean to the conclusion that the effort is not worth the result. Dukat was heavily focused on by JB in 2019 and they didn’t make it. Maybe AI might help, but the there is a fundamental mismatch in expressiveness. And you can monkeypatch it how you want, but imho that only results in unwieldy APIs. https://resources.jetbrains.com/storage/products/kotlinconf2019/slides/Thursday/Aud%2015/S.%20Aigner,%20The%20State%20of%20KotlinJS.pdf
e

Edoardo Luppi

03/15/2024, 6:38 PM
I mean, if the effort wasn't worth the results we wouldn't have gotten all the types under kotlin-wrappers. Definitely non an easy job, but with patience and compiler plugins, a human can do it. A standalone tool, maybe.
a

Arjan van Wieringen

03/15/2024, 6:38 PM
That’s fair
So there is your training set ;)