Kotlin -> Readable Scala?

I have been working with Scala code (more of a maintenance work) for quite some time now. Most of the code we work on is Java then some in Groovy and few micro-services are written in Scala. Initially we (Java Developers) used to avoid working on Scala codebase and used to keep extra buffer while estimating efforts around Scala codebase. Reason was obvious- Scala was alien for us who were comfortable with verbosity of java and functional code was there to make it worse.

Fast forward to present, I am doing Coursera’s Scala Course by Martin Odersky (thanks Sujit Kamthe for recommending this excellent course). I am still struggling to get proper hold of Scala yet and still started learning Kotlin! While I am doing this dual learning together - I can’t avoid the striking similarities they both share. Let’s explore them on the readability scale.

  1. Null Checks- handling optional values

In my opinion, Groovy has really mastered null checks with safe navigation operator (“?”) and Kotlin borrows it for good. “?” is much more natural to express possible emptiness compared to Option[ ] of Scala.

“?” is much more natural and intuitive compared to Optional in Scala

As you can see below, Kotlin allows you to use “?” to define optional values and it has compile time check.

val firstName: String = null //This fails to compile
val lastName: String? = null //this one compiles successfully

You can combine it with elvis(?:) operator also.

val addressLine: String? = null
val addressLength = addressLine?.length ?: -1

addressLength would be -1 for null string or actual length otherwise.

Whereas in Scala, you wrap it inside option- which needs unwrapping whenever you need raw value

val firstName: Option[String] = None
println(firstName.get) //unwrap

2. Case Class / Data Class

Scala’s case classes offer powerful mechanism for creating immutable objects which gracefully decompose in pattern matching, Kotlin has something similar — Data Classes. Kotlin unfortunately doesn't support powerful pattern matching like in Scala though.

Both are equal in terms of readability but Scala is clear winner when it comes to functionality around case classes.

In Scala and Kotlin you write them the same way as below

//Scala
case class Person(firstName: String, lastName: String)
//Kotlin
data class Person(val firstName: String, val lastName: String)

Kotlin supports these nifty Destructuring Declarations, similar to tuple extractors in Scala

val (firstName, lastName) = person // Kotlin
val (_, lastName) = person // when you don't worry about firstname

Also you can easily clone them in Kotlin just like Scala

val cloneOfPerson = person.copy()
val personWithAmendedFirstName = person.copy(firstName = "Some Other Name")

You can even clone nested objects

val personWithDifferentAddress = person.copy(address = person.address.copy(city = "new city"))

3. Implicit Parameter in Lambda Functions

Scala has underscore “_” to refer lambda parameter inside function. Kotlin has groovy like “it”.

I feel Scala’s ‘_’ is concise compared to Kotlin’s implicit ‘it’, but Kotlin wins for functionality

In Scala we use underscore as

personList.find(_.name == "Name To Find")

in Kotlin

personList.find {it.name == "Name To Find"}

Kotlin’s implicit parameter has advantage over Scala that it can be used multiple time.

// Doesn't compile in scala
personList.find(_.name == "SomeText" && _.age > 18)
//Whereas in Kotlin it works :)
personList.find { it.name == "SomeText" && it.age > 18 }
There are many more features we can discuss about, but overall I feel Kotlin is quite readable for java developers compared to Scala. Kotlin has occupied the space between Scala and Java and it’s a simple transition path for Java developers. Even for enterprises, little learning curve and Java inter-operability of Kotlin simplifies adoption path.
Like what you read? Give Ashay Thorat a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.