Kotlin universal?

I recently started working on a github project which uses python for scripting throughout

It’s extremely difficult to introduce kotlin (even though I really want to!) due to the fact that the migration would be extensive & very expensive (no simple way to do piecemeal), due to usage of pip (no gradle integration)

I am a huge believer that Kotlin & Rust can replace all [general purpose] systems & environments out there - but in this case, it’s complicated

Given that Kotlin is already beautifully designed with the idea of compile-time environments (expected / actual declarations), and that it already supports compiling to 3 different envs (JS, native, JVM), it seems like it would be straightforward to expand this to be universal, i.e. to support python, dart/flutter, ruby, as target environments

Even if at first there are no common library implementations, e.g. collections, it seems like something the community would/could help with

Interestingly, someone has already taken a stab at this for dart/flutter:

1 Like

Figure it can’t hurt to file a request :rofl:

https://youtrack.jetbrains.com/issue/KT-72533/Kotlin-Universal-Kotlin-for-every-major-environment

Replacing a script language would be really hard. How do you see that working?

1 Like

JavaScript is a script language, is it not?

Kotlin/JS compiles to JavaScript beautifully

I don’t think it’d be very difficult to do in these other languages, even if at first it just does transpilation

You may need a different tool for the transpilation, but the goal here is:

  • Produce something which is able to call underlying environment libraries, e.g. existing Python libraries
  • Produce something which is universal - for example, you could have a single XML parser library, written in Kotlin, usable from: JavaScript, JVM, ObjC, Python, & Flutter/Dart

Supporting additional languages wouldn’t be “easy”. Language ecosystems represent an incredibly large surface area, and so there is lots of engineering involved. That said, in the case of python, I agree 100% that it would be worth the effort, given that Python is the language of AI and being python-compatible would give Kotlin a huge foothold into that rapidly growing ecosystem.

1 Like

Apologies, probably poor choice of words - I meant “straightforward” rather than easy (obviously it’s involved or I’d have done it already :rofl:)

To be clear though, I think there’s 2 steps here, one straightforward, the other quite large:

  1. Transpiling to python / dart
  2. Ecosystem support (e.g. collections, i.e. the actual classes)

Just having (1) would already be hugely beneficial & start moving the needle, whereas (2) is obviously a ton of work (but not a blocker)

1 Like

You may be interested in reading through kotlinlang #python. Two people started Python integration, but it’s a lot of work. I think it’s paused at the moment. If you want to help, don’t hesitate to post there and contribute!

1 Like

Oh nice, thanks for the link!

I searched everywhere but couldn’t find it

I do wonder if that isn’t part of the problem, which is the advantage of an officially-supported offering: if people don’t know about it, they won’t contribute, it dwindles & dies.

That’s true, but once they start advertising it, people expect it to work fairly well, otherwise it gives a bad reputation to their other non-stable projects.

1 Like

Maybe as an alpha release or something like that :rofl:

Remember here that stability, in this case, is somewhat different - the thing most people care about in “working fairly well” is:

  1. It doesn’t crash constantly
  2. It doesn’t completely change & require you to rewrite everything

It’s unlikely that either of those can happen here, because:

  1. It’s transpilation: it has to pass through the 2nd compilation, too
  2. You’re writing basic kotlin code & simply expecting it to compile into something Python

My understanding is that in addition to scripting nature, the main advantage pushing Python is that it easily interacts with C-libraries.
Using Kotlin/Native already interacts with C-libraries and can be compiled as such.
Kotlin/Native also could be compiled into C-library and in that form should be easily called from Python.

How about start with rewriting a Python script in Kotlin, using the same C-libraries and if need to call other Python scripts as external executables? This sounds to me like the path to convert Python scripts into Kotlin one-at-a-time.