kotlin for rubyists
Although I’ve never seen Ruby cited as an inspiration for Kotlin, ever since I first ran into it, I couldn’t but notice how closely it resembles Ruby.
Kotlin shares many of Ruby’s niceties: you can do a lot with a very small amount of code; its standard library includes many useful tools that help you express your intent clearly; it can be used to create beautiful domain specific languages; it has an interactive shell; and it includes a number of functional constructs. Even its syntax is very Ruby-like at times — it has a block-lookalike!
So if they share many similarities, what does Kotlin bring to Rubyists? Quite a bit, actually.
If you find yourself needing to work or interoperate with a Java environment, Kotlin and its tooling were built to allow easy migration from Java. It’s very easy to incrementally move a codebase from Java to Kotlin, and there are even automatic translation tools. Yes, this also means that Kotlin can make use of all the niceties of the Java and JVM environment: just-in-time compilation, state-of-the-art garbage collection, first-class concurrency tooling, and its many, many libraries and frameworks. Additionally, it is also a first-class citizen and officially supported by Google as an alternative to Java when building Android applications.
For those seeking static validation, Kotlin is both statically typed and null-safe. Static typing ensures that a number of common errors happen at compile-time, when you’re preparing your code for execution, rather than at runtime (no more mysterious NoMethodError
s!). Null safety means that the compiler can distinguish between nullable and non-nullable types, and is able to enforce that if before calling a method on something that can be null
(or nil
), you’ve checked if the value is not null
. Kotlin’s type system also includes a number of other niceties, such as zero-overhead inline classes that work as a kind of aliases to existing classes — e.g. you’ll be able to get a compile-time error if you try to assign a Password
value to an Username
field, even if both are actually compiled as String
.
Kotlin can also be used for front-end development: It can be compiled into javascript code and easily work with common javascript libraries, it can understand TypeScript definitions, and can also by used on Node.js.
Last but not least, Kotlin can also be compiled — using LLVM — into an optimized machine code executable. This feature is still experimental, and it allows interoperating with compiled languages such as C/C++/Objective-C/Swift.
In short, although I remain a committed Ruby lover π, I believe that Kotlin deserves a place in every Rubyist’s toolkit.
Curious to check it out? At the 2019 edition of the awesome Ruby on Ice conference, I presented a workshop called “Kotlin for Rubyists”.
This workshop covered not only an introduction to the language, but also to the ecosystem: how to install it, which build system to use, testing, performance tooling, etc.
We also build several example applications (including a nice microservice). I’ve made available all of the example material on this git repository.
You can find the slide deck from the workshop embedded below:
Feedback is very welcome π