January 2019
Intermediate to advanced
392 pages
10h 11m
English
The switch { ... } control flow element is replaced by when { ... }. The when { ... } element of Kotlin is much more flexible than the switch { ... } element in Java, because it can take a value of any type. A branch only has to contain a matched condition.
The following example demonstrates how to use when { ... } as a statement:
fun whenStatement() { val x = 1 when (x) { 1 -> println("1") 2 -> println("2") else -> { println("else") } }}
The preceding code snippet contains the else branch, which is optional for a case with a statement. The else branch is invoked if all other branches don't have a matching condition. The else branch is mandatory if you use when { ... } as an expression and the compiler can't be ...
Read now
Unlock full access