When
Instead of the widely used switch
statement, Kotlin provides the more flexible and clear when
construction. It can be used either as a statement or as an expression.
When Statement
fun main() {
cases("Hello")
cases(1)
cases(0L)
cases(MyClass())
cases("hello")
}
fun cases(obj: Any) {
when (obj) { // 1
1 -> println("One") // 2
"Hello" -> println("Greeting") // 3
is Long -> println("Long") // 4
!is String -> println("Not a string") // 5
else -> println("Unknown") // 6
}
}
class MyClass
- This is a
when
statement. - Checks whether
obj
equals to1
. - Checks whether
obj
equals to"Hello"
. - Performs type checking.
- Performs inverse type checking.
- Default statement (might be omitted).
Note that all branch conditions are checked sequentially until one of them is satisfied. So, only the first suitable branch will be executed.
When Expression
fun main() {
println(whenAssign("Hello"))
println(whenAssign(3.4))
println(whenAssign(1))
println(whenAssign(MyClass()))
}
fun whenAssign(obj: Any): Any {
val result = when (obj) { // 1
1 -> "one" // 2
"Hello" -> 1 // 3
is Long -> false // 4
else -> 42 // 5
}
return result
}
class MyClass
- This is a
when
expression. - Sets the value to
"one"
ifobj
equals to1
. - Sets the value to one if
obj
equals to"Hello"
. - Sets the value to
false
ifobj
is an instance ofLong
. - Sets the value
42
if none of the previous conditions are satisfied. Unlike inwhen
statement, the default branch is usually required inwhen
expression, except the case when the compiler can check that other branches cover all possible cases.