| Start |
|---|
| When-statement argument matching When given an argument, the The when statement also knows some more advanced matching options: |
| When-statement as expression Like if, when can also be used as an expression: To be used as an expression, the when-statement must be exhaustive, i.e. either have an else branch or cover all possibilities with the branches in another way. |
Standard if-statement The else-branches are optional in normal if-statements. |
| If-statement as an expression If-statements can be expressions: Note that the This can also been done with a multi-line variant with curly brackets and multiple TIP: Kotlin can infer the type of the variable for you but if you want to be sure of the type just annotate it on the variable like: |
| When-statement instead of if-else-if chains The when-statement is an alternative to an if-statement with multiple else-if-branches: Same code written using an if-else-if chain: Just like with the if-statement, the else-branch is optional, and you can add as many or as few branches as you like. You can also have multiline-branches: |
| When-statement with enums
As you can see in second case line ( If your cases are not exhaustive the compile will show an error. You can use Though the same can be done using Check here (opens new window) for more information about kotlin RemarksIn contrast to Java's |
Read Article
https://develop.horje.com/learn/1434/reference