|
Kotlin has when, which is similar to switch in other programming language.
|
|
|
When can be used in a way similar to a switch statement in other languages.
|
else -> println("x is neither 1 nor 2")
|
|
However, like if, it can also be used as an expression.
|
println("result = $result")
|
|
Multiple matches can be used for a single branch.
|
0, 1 -> println("x is 0 or 1")
else -> println("x is not 0 or 1")
|
|
It is also possible to omit the argument to when. In this case the when block becomes similar in use to an if block.
|
x % 2 == 0 -> println("x is even")
x % 2 != 0 -> println("x is odd")
|
|
A when block has several "special" syntax for matches. For example, it has support for ranges.
|
in 1..10 -> println("y is in the range 1..10")
!in 10..20 -> println("y is outside 10..20")
else -> println("y is something else")
|
|
Type checking with is is also supported.
|
fun describe(obj: Any): String =
is String -> "String of length ${obj.length}"
println(describe("hello"))
|