Meet Kotlin

by

Kotlin is a JVM language that hit version 1.0 about a year ago (February 2016).
It is developed by JetBrains, the same people who make my favorite suite of
IDEs. The language itself is open-source under the Apache License 2.0 and is
developed as a community project over at kotlinlang.org. Kotlin is something
that I have become rather excited about over the past year. This post’s goal is
not to teach you Kotlin but to get you excited about it!

Goals of Kotlin

The stated goals of Kotlin are: Interoperability, safety, and tooling.

Interoperability

Kotlin is designed to be 100% interoperable with Java. There is always the
ability to use Java libraries from Kotlin, just as there is the option to use
Kotlin libraries from Java. It is even extremely easy to mix Java and kotlin
within the same codebase without a problem. See the documentation for
more info regarding interoperability

Safety

Type safety is important to the design of Kotlin as well. It makes an attempt to
catch type errors at compile time in order to avoid runtime instability.

Tooling

While Scala has many of the features that the Kotlin team wanted in a language,
it took significantly longer to compile and had significant runtime
dependencies. Kotlin has a stated goal of being as fast to compile as Java and
does not have a runtime dependency (though it does need its standard library,
I think.)

There are official Kotlin plugins/support for IntelliJ IDEA (community and
ultimate), Android Studio, Gradle, Maven, Ant, even Eclipse.

Syntax

In Kotlin the type comes after the name. If you use Typescript, Go, Swift, etc
you’ll be familiar with this concept.

Additionally, var is non-final, but a val is final

Functions are not required to belong to a class, and are defined with a fun
keyword.

Where can I use Kotlin?

You can use Kotlin (nearly) anywhere you are already using Java. It can be added
into a backend project relying on Spring or Play frameworks with ease. It is
also gaining huge popularity in the Android development community.

Kotlin on Android is what drew me to the language in the first place. I, along
with much of the Android development community, had a growing desire for a less
verbose, more concise, more modern language to build our applications with. The
fact that Android Studio fully supports Kotlin out of the box is a huge help to
knocking down the barriers of adding Kotlin into an existing codebase, as well
as making it easy to start new projects with Kotlin.

Language Features

Now for the fun part: showing off the features of Kotlin. My favorite way to
introduce Kotlin is not to answer “what can I do with Kotlin?”, but instead
answer “what don’t I need to do in Kotlin?”.

What don’t you need to do in Kotlin?

Pretend you need to make a lightweight model class to represent a person. For
simplicity’s sake, a person can have a name, age, and a city. You want to be
able to compare two person objects (equals() and hashCode) as well as print
out the model for logging or debugging reasons (toString()).

In Java this lightweight model looks something like this:

We have 40 lines of code to achieve this simple and common task. Kotlin can do
all of this using its data class functionality… in one line.

A data class has final fields defined after the class name, it generates
(internally) the constructor, equals()/hashCode(), toString(), copy()
(not used in my example), and componentN() functions

Safety

A value must be assigned either at the time of declaration or during instance
creation. If not, it will throw a compiler error

To allow for a value to potentially be null, you must use the ? operator on
the type. If you have a potentially null type, you must safe-call (?.) or
assert that you know more than the compiler (!!.)

Other nifty features

Destructuring

Single value functions

Lambdas and iterators

Extensions

To me, this is quite similar to “monkey-patching” in Ruby. Officially, it draws
inspiration from C# and Gosu.

Here is a simple example of adding a .reverse() method to Strings.

There is so much more…

This is hardly an exhaustive showcase of Kotlin’s features. I hope that it was
able to get you excited or thinking about trying something new on a project
though! If you’d like a relatively safe way to get started, try dropping kotlin
into an existing project and using it to write some tests. Your tests will
become shorter, more clear, and you’ll hopefully want to use Kotlin everywhere!

I’m intending to use Kotlin any time I have the opportunity. I have high hopes
for this young language that shows lots of promise, hopefully you’ll join in on the
excitement!


1 Comment

  1. Nathaniel Powers on said:

    Thanks for the quick write up! I’ve been looking into switching over to Kotlin from Java for my Android work. Sounds like the Kotlin team scores another point today.
    For anyone reading this who wanted a good resource to learn more, I found this super helpful:
    https://caster.io/courses/introduction-to-kotlin/

Leave a Reply

Your email address will not be published. Required fields are marked