What is Kotlin

Kotlin, it has been described as the upstart coding language conquering Silicon Valley. Pinterest, Basecamp, Square, and even Google have thrown their support behind it. The biggest advantage of Kotlin is that it’s 100% interoperable with Java and Android. Created by JetBrains, the people behind IntelliJ, Kotlin was fist developed as a solution to their internal issues with Java. They took the lessons learned from C#, Scala, and others to make Kotlin a powerful functional language.

My exposure with Java really took off when I started working in a Grails framework, which uses Groovy - a multi-faceted language for the Java platform, and dabbling in Scala. This is what I’ve learned so far in my Kotlin education.

Functions are in the lead

In Kotlin, we are elevating even simple expressions as a function, even the if statement is an expression. The if statement could also be assigned to a variable, similar to the ternary statement (expression) ? true : false; however treating the if statement as a function allows us to use more complex if else statements without having to create a confusing nested ternary statement.

    return if (expression) {
        "This was true"
    } else if (another) {
        "This is something else"
    } else {
        "This was false"
    }

Kotlin uses unchecked exceptions to let us code without having to worry about various exceptions. The exceptions are actually thrown in Kotlin, but we don’t have to catch exceptions or specify which exceptions our code throws. In this way Kotlin handles errors similar to events - they are triggered but do not do anything implicitly. We can handle exceptions in try/catch blocks if we chose to do so. The try statements are similarly structured to the if statements.

    val number:Int? = try {
        Integer.parseInt(x)
    } catch (e:NumberFormatException) {
        null
    }

In this block, there are several Kotlin concepts working together, so I’ll step through it. First, we are defining a variable number as a type of Int; however, see the question mark on the end of the type? That’s the Kotlin way of telling us that number is a nullable variable. Next, we are attempting to parse the value x as an Integer. Since that parse method could fail if x is undefined, we can wrap it in a try/catch block. If we experience the error NumberFormatException, we can just set number to null, since it’s a nullable Int.

In this way, we are not reinstantiating the variable number, and our code better off for it. We are not dealing with exceptions outside of the context of our setter - it’s all neatly contained, cleaner, and easier to understand.

While Loops

Kotlin is very similar to Java when talking about while loops. The big difference is that Kotlin loops are based on ranges within an iterator - that could be lists, or even key:value objects. If we want to expose the index, we can simply add withIndex() to the collection. Ranges are a very powerful thing in Kotlin.

    for(i in 1..10) {}
    // full close
    for(i in 1..10 downTo 1 step 2) {}
    // half close
    for(i in 1 until 10) {}
    // with index
    var numbers = 5..10
    for((index, number) in numbers.withIndex()) {}
    for((name, age) in ages) {
        println("$name is $age")
    }