Lanky Dan Blog

Privately scoped variable in `when` block

January 03, 2019

kotlin

Super short post, on a change introduced in Kotlin 1.3 (yes I know it has been out for a while now). We will take a quick look at capturing the subject of a when block into a scoped variable. This is a quality of life improvement that saves a line or so of code while making the role of the variable clearer.

Below is what you would write before the change:

val enum = myClass.enum
when (enum) {
  MyEnum.ONE -> println(enum.propertyOne)
  MyEnum.TWO -> println(enum.propertyTwo)
  else -> println(enum)
}

You can now write this instead:

when (val enum: MyEnum = myClass.enum) {
  MyEnum.ONE -> println(enum.propertyOne)
  MyEnum.TWO -> println(enum.propertyTwo)
  else -> println(enum)
}

The instantiation of the enum variable is merged with the declaration of the when block. enum is now scoped to the when block and cannot be accessed outside of it. Not a massive change, but it does makes the code look a little tidier.

Let’s look at one more example:

val obj = someObject()
when(obj) {
  is String -> println("This is a string and it says $obj")
  is Number -> println("This is a number and its value is $obj")
  else -> println("I don't know what $obj is")
}

Becomes:

when(val obj = someObject()) {
  is String -> println("This is a string and it says $obj")
  is Number -> println("This is a number and its value is $obj")
  else -> println("I don't know what $obj is")
}

Not really anything else to say on this subject as there isn’t much to talk about in the first place. You probably get the picture, the example from the Kotlin 1.3 Release Notes might make sense to you if I have failed to explain it myself.

If you found this post helpful, you can follow me on Twitter at @LankyDanDev to keep up with my new posts.


Dan Newton

The potential traps in Kotlin's Data Classes

August 17, 2019
kotlin

The aim of this post is not to point out some massive flaws in Kotlin’s design of data classes and show you how to get passed them. Actually…

Connecting a Ktor web server to a Corda node

August 12, 2019
cordakotlindltdistributed ledger technologyblockchain

The preparation for this blog post began several weeks ago (probably over a month by now). Before I could write about melding Corda and Ktor…

Flows can do anything

July 30, 2019
cordakotlindltdistributed ledger technologyblockchain

In Corda, Flows can do a lot more than proposing new transactions to record between organisations. Although, saying they can do anything…

Ktor - a Kotlin web framework

July 24, 2019
kotlinktor

Ktor is an asynchronous web framework written in and designed for Kotlin. Allowing the more impressive features of Kotlin, such as…

Saving transactions where only a subset of parties are signers

July 05, 2019
cordakotlindltdistributed ledger technologyblockchain

It took a while for me to think of a title that could summarise the contents of this post without becoming a full sentence itself. I think I…