Source code converter from Python to Kotlin?

Hello,
I am very new to Kotlin but I can code in Python as well as Java, C# and C.
Prior to posting here I did Google for the answer but alas did not find anything.

As you may know IntelliJ IDEA provides a tool that converts Java source code into Kotlin source code.
I just used it quite successfully on one of my Java projects.

I am looking for something similar that converts Python source code into Kotlin source code.
I hope this is the right forum, please advise.

Many thanks.

I do not believe there is one, and possibly there will never be. Java to Kotlin converter is possible because they have similar structure and are based on the same libraries (at least for JVM). Python on the other hand has completely different structure. It is a dynamic language and it strongly affects the programming style, so even if someone would write a converter, it would produce ugly and non-ideomatic kotlin code.

Could you probably explain your use case? Maybe there is a better way.

2 Likes

many thanks!

More background.

I was just looking for some tools to help migrate existing Python code to Kotlin, it does not need to do 100%, even if it does 75%, it may help.

For example - we have a lot of Python/PySpark running on Apache Spark which is a JVM ecosystem.
It would (IMO) help the adoption of Kotlin in Big Data Apache Spark ecosystem to be able to convert PySpark programs (i.e. Python Spark) code into Kotlin Spark.

I believe Kotlin can run on Apache Spark due to its 100% compatibility with the JVM on which Scala/Java run. So this is my primary use case - migrate from PySpark to Kotlin on Apache Spark.

You are correct about posibility of using Spark there are even some examples out there and some discussions in kotlin slack in datascience and science channels. Still, I do not think that automatic conversion is possible. You can try to use something like this and then convert to kotlin via IDEA converter, but I would not recommend it. Better to rewrite everything from scratch. My phd student has some experience of rewriting pyspark code into kotlin, so he could probably help.

1 Like

thank you very much.

As a second option - a detailed document/whitepaper on how to use Kotlin in Apache Spark with emphasis on moving PySpark code and perhaps even Scala code to Kotlin would be helpful.
I did find some example in github using Kotlin in Apache Spark but they are quite small and thin.

Python is also popular in Ml/AI because it can make use of numerous C/C++ libraries built for ML/AI to which Python can interface easily, giving it the edge over other languages.

I am guessing Kotlin popularity in that space could also be lifted by some tooling and some documentation on how to use AI/ML libraries from Kotlin, where the libraries are written in C/C++.

Thomas Legrand just posted a link to his article about it in slack: https://www.thomaslegrand.tech/kotlin/2019/04/07/machine-learning-kotlin.html. As for spark for kotlin, you can use any Java tutorial as is (because of full compatibility). There is also this project for REPL. PySpark - is a subset of more powerful Scala/JVM API which also have some limitations due to Python interpreter deficiencies. I think you will love it when you try.

As long as you do not need hardware-optimized performance, JVM gives you the whole spectrum of tools for ML with much better quality and convenience. Of course, in some cases, you need something super-optimized to work on a hardware level, then your only solution is C/Fortran.

1 Like

Use ChatGPT. Just type:
“Convert the following python code to kotlin:”
…

Note, ChatGPT will only generate a portion of the code. To generate all of the code you paste in, you need to type “more” after it’s done a section.

The other limitation is that if you paste in too large a code, it will not be able to convert all of it. Just break the code into smaller parts but at sections where the code’s syntax doesn’t break.

You might get some ideas from ChatGPT, but for any non-trivial code it’s unlikely to give Kotlin code that compiles, let alone does what you want it to do.

GPTs have no real understanding of what they output — they’re merely designed to produce text that looks plausible. And while for natural language it’s sometimes not so easy to tell the difference, for source code it tends to be more obvious!