23 September 2016

First Look At Kotlin


Has been a long time since I found an interesting programming language which changes the way you approach solving problems in certain areas. Last time I was into JavaFX Script however it didn't take off as expected mainly due to the Java community not embracing it in general, the language was too specific (domain orientated), and not enough resources were being allocated to improving/supporting it. Also when trying to use JavaFX Script in projects the interoperability with the Java ecosystem (JPA, JAX-RS etc) often ended up being a show stopper.

By chance when looking at alternatives for Android development I discovered Kotlin. Kotlin is reasonably easy to learn with its difficulty sitting in between Python and Java. What ended up being really attractive about the language is its pragmatism/industry driven approach to software development. Many features in Kotlin are very well thought out with every single one being a result of Software Engineering experience in the industry (ICT). There aren't many programming languages that manage to balance conciseness with readability. You end up more often than not reading rather than writing code.

One of my favourite Kotlin features is Delegated Properties, specifically storing properties in a Map. Below is an example of this:




Text Version:

class Person(val map: MutableMap){
     var firstName by map
     var lastName by map
     var age by map
}

fun main(args: Array){
     val map = mutableMapOf(age to 20, firstName to Joe, lastName to Bloggs)
     val aPerson = Person(map)

     map["lastName"] = "Burke"
     aPerson.firstName = "Jane"
     println("Person: ${aPerson.firstName} ${aPerson .lastName}, ${aPerson .age}")
     println(Person age is Int: ${aPerson.age is Int})
     println("Map -> First Name: ${map["firstName"]}")
}



What you have above is an example of bidirectional Data Binding (sort of), which opens up a number of interesting real world applications like binding a database model (the model class, not the actual database) to a view. Despite the MutableMap using mixed types for the values it works just fine for the Person properties that rely on type inference via delegation. Blogger unfortunately doesn't have syntax colouring support for Kotlin code, is Google planning to fix this?

Some people view Kotlin as a statically typed version of Python however it isn't entirely warranted. If anything Kotlin is most similar to Swift. Even the language goals are very similar:

  • Concise
  • Safe
  • High performance
  • Versatile
  • Tooling
  • Interoperable

Kotlin documentation isn't too bad (the quality of the references is good) however it is seriously lacking a decent tutorial. Confusingly the navigation of the documentation section of the Kotlin website is all over the place. The Kotlin Koans for instance is in the Tutorial area instead of the More resources area. There appears to be a “Getting Started Guide” in the Reference area. If it is a tutorial then it needs to be moved to the Tutorial area.

Some Kotlin design decisions are questionable/controversial. Closed (sealed) classes are one decision which is very odd considering that there are a significant number of libraries/frameworks that rely on open class design, and Software Engineers frequently design software using an open class architecture anyway. Another one is Data Classes where some implemented functions are included for free (toString, equals, hashCode), and object de-structuring is automatically supported, which should be in ordinary classes instead. Using Data Classes causes severe issues (no class inheritance etc) that would be easily avoided by going with ordinary classes anyway.

Below is a list (not exhaustive) that shows who is using Kotlin:


Companies

  • Amazon
  • Google
  • Netflix
  • NBC (NBC News, TODAY, and Nightly News Android apps)
  • American Express
  • Pivotal (own Spring framework)
  • Expedia
  • Square
  • 3D Robotics (3DR Tower Android app not available in Google Play)
  • Basecamp (creator of “Ruby On Rails”), Basecamp Android app
  • Prezi
  • JetBrains
  • MongoDB
  • BQ (various Android apps not available in Google Play)
  • Meizu (various Android apps not available in Google Play)
  • App Foundry
  • GMC
  • Allegro Group
  • Bryx 911 (first responder Android app not available in Google Play)
  • Trapit (builds Android apps for some fortune 500 companies using Kotlin)
  • Farm Logs (Android app not available in Google Play)
  • Nulab Inc (Android app not available in Google Play)
  • Pinterest

Software Projects

  • Anko (official Android application development library for Kotlin)
  • Kotter Knife (Android view binding library for Kotlin)
  • TornadoFX (JavaFX library for Kotlin)
  • MapDB (embedded NoSQL DB for the JVM)
  • Requery (SQL query and persistence library which has Kotlin support)
  • Exposed (official SQL library for Kotlin)
  • Spec (official unit testing framework for Kotlin)
  • Kara (web framework for Kotlin)
  • Quasar (JVM concurrency library which has Kotlin support)
  • Spring Boot (micro services framework which has Kotlin support)
  • Gradle (version 3 onwards has support for developing plug-ins using Kotlin)
  • RxKotlin (RxJava bindings for Kotlin)

Android Apps

Kotlin Jobs (Possible Users)

One of the major surprises with Kotlin is the significant interest in the language from the Android Development community. Certainly helps when Jake Wharton (likely the most well known person in the Android Development community) is endorsing Kotlin. Many in the community want Google to support Kotlin as an alternative to Java.

In Fragmented postcast #20 an out of blue question was asked by one of the presenters, “When is Google going to support Kotlin in Android Studio?”. Why hasn't Kotlin been covered in an Android Developers Backstage podcast yet? Since late last year there have been a few Googlers frequently visiting the Kotlin slack channel, which has recently celebrated reaching the 4000 registered users mark.

As you can see it is highly unlikely Kotlin will be going away considering the sheer number of major companies and Android apps using Kotlin. If anything this shows that all Java developers should be seriously looking into using Kotlin for some projects if they haven't done so already. Kotlin is highly likely to represent the biggest change to the JVM landscape after Java 8.