DEV Community

Fırat Küçük
Fırat Küçük

Posted on


Kotlin Native Experiment

I don't know why but I really love simple executable programs. Maybe it's just because I am dealing with Java stuff and missing fast and small applications. Executables without any runtime dependencies. That's why Go and Rust languages blink. No offense but I don't like Go's exception handling and Rust syntax is pretty weird. More human-readable languages like TypeScript and Kotlin seems better. Thanks to LLVM and GraalVM, our programming languages and applications are faster than ever.

Microservices age triggers the micro-sized and ultrafast applications and frameworks. Micronaut, Quarkus, Helidon, and Spark some of the great examples of that era. Thanks God, all those fancy toys and spring boot supports Kotlin.

Kotlin is one of my favorite programming languages. To be honest, at the beginning I refused to accept the variable definition syntax.

val myVariable: String = "hello"
Enter fullscreen mode Exit fullscreen mode

I find classical Java-style more convenient. For instance when you write CarFactory IDE auto-completes carFactory variable name in Java-like programming languages.

CarFactory carFactory = new CarFactory();
Enter fullscreen mode Exit fullscreen mode

But despite its unusual variable declaration, there're plenty of cool things in Kotlin. Immutable data types, data classes, by default final classes, co-routines, and many more. I am determined and focused on the journey of Kotlin. So here we're. Gonna try a Kotlin native application.

Currently, most of the Kotlin native examples are based on IntelliJ IDEA. Well... I didn't like this idea. It should be as simple as possible. So I tried to download a native compiler. But currently, it's really difficult to obtain one. Either you should compile or you should use IntelliJ IDEA.

So I found out the executables URL. You can download them for 1.3.61 version.

After downloading and extracting the content you can compile via command-line. Try to save this hello world program as hello.kt

fun hello(): String = "Hello, Kotlin/Native!"

fun main() {
Enter fullscreen mode Exit fullscreen mode

and then let's compile it.

./kotlinc -o hello hello.kt
Enter fullscreen mode Exit fullscreen mode

Kotlin compiler is gonna compile needed dependencies. After compilation completed we can try our executable.

Enter fullscreen mode Exit fullscreen mode

It's 387KB a little bit bigger than a standard C/C++ application. But it's Ok. :) It's not a statically compiled executable. The target is dynamically linked.

Top comments (0)