📜  Kotlin when - Kotlin (1)

📅  最后修改于: 2023-12-03 15:02:32.326000             🧑  作者: Mango

Kotlin When Expression

when is a powerful and expressive control flow operator in Kotlin, that allows you to replace traditional if-else or switch statements in a concise and readable way. In this article, we'll explore how you can use when in different situations.

Basic Syntax

The basic syntax of a when expression is as follows:

when (value) {
    match1 -> { /* code */ }
    match2 -> { /* code */ }
    match3 -> { /* code */ }
    else -> { /* code */ }
}

Here, value is the value that you're testing, and match1, match2, and match3 are the possible values that you want to compare it against. The else block is called if none of the values match.

Matching Values

when can be used to compare values of different types, including constants, variables, functions, and even ranges. Here are some examples:

val x = 25

when (x) {
    1 -> println("One")
    2, 3, 4 -> println("Two, three, or four")
    in 10..20 -> println("Between ten and twenty")
    is Int -> println("An integer")
    else -> println("Something else")
}

In this example, we're testing the value of x against several conditions. The first condition matches if x is equal to 1. The second condition matches if x is either 2, 3, or 4. The third condition matches if x is between 10 and 20, and the fourth condition matches if x is an Int type. The else block matches if none of the conditions are satisfied.

Combining Conditions

You can also combine conditions in when expressions using logical operators. For example:

val a = 5
val b = 10

when {
    a > b -> println("a > b")
    a < b && b < 15 -> println("a < b and b < 15")
    else -> println("Something else")
}

In this example, we're testing multiple conditions without comparing them to a value. The first condition matches if a is greater than b. The second condition matches if a is less than b and b is less than 15. The else block matches if none of the conditions are satisfied.

Smart Casting

when expressions also support smart casting, which is a feature that automatically casts an object to a specific type if it matches a condition. For example:

fun printLength(obj: Any) {
    when (obj) {
        is String -> println(obj.length)
        is List<*> -> println(obj.size)
        is Map<*, *> -> println(obj.keys.size)
        else -> println("Unknown type")
    }
}

In this example, we're using when to check the type of obj and call different methods based on the type. If obj is a String, we're calling the length property. If it's a List, we're calling the size method. If it's a Map, we're calling the keys.size property. If obj is none of these types, we're printing "Unknown type". The is keyword here acts as a check that obj is of the given type.

Conclusion

when expressions are a powerful and expressive feature in Kotlin that can replace traditional if-else or switch statements. They support testing values of different types, combining conditions, and smart casting. With when, you can write concise and readable code that's easy to understand and maintain.