Content
Summary of ‘when’ usage cases
Source - Kotlin in Action
What is ‘when’?
A more powerful version of Java’s switch.
Java switch
- Improves readability of if-else branching by using switch.
- However, branch arguments are limited to constants (mainly enums, strings).
Kotlin when
- Branches can use not only constants (enums, strings) but also arbitrary objects and expressions—no restrictions.
- A universal branching statement.
Use Cases
1. Branching on enums to return a specific string
enum class
|
|
when code
|
|
- No need for break; can use arrows
- With arrow functions in Java switch, this is now similar in Java
Up to here, you might think “Isn’t this just like Java?”
2. Listing multiple arguments in a branch
|
|
- Can list multiple values in a single branch
- Also now possible in Java switch
Still seems similar to Java.
3. Using objects as arguments
|
|
- Can use arbitrary objects as branch conditions
- Checks conditions in order from the top
- Uses equals for object comparison
Here, the difference from switch becomes clear.
4. Using expressions
|
|
- No argument! Major difference from switch
- No need to create a set object, so can improve performance (though readability may decrease)
- Can use expressions as branches!
- The result of the expression must be Boolean.
At this point, it seems functionally similar to if-else. For most branching logic, you can use when.
5. Example using smart casts
What is a smart cast?
- If you check an object’s type, the compiler automatically casts it
is
=instanceof
Example
- Expressing operations like 1+3+5 in an object-oriented way (tree structure)
- Class relationships:
- Expression type ← Num type (operand)
- Expression type ← Sum type (operator)
|
|
- No need for explicit type casting thanks to smart casts
- In Java 14+, similar pattern matching is available in if statements - link
- Using when instead of if improves readability
- If you declare as a sealed interface, else is not required.
6. Using in
for ranges
|
|
- Can use not only smart casts but also
in
for ranges, showing the versatility of when.
Summary
If you have a lot of branches in if-else, use when.
Advantages
- Improves readability by removing if-else keywords
- Can use smart casts,
in
, and more - Can be used for all branching logic
- If you list all enum values, the compiler checks at compile time, so no need for a default branch
Disadvantages
- What are they?
- For enums, else is required, so you must write it
- If not needed, write
else -> Unit
- If not needed, write
- For enums, else is required, so you must write it
TMI - Refactoring the Expr Example
From an OOP perspective:
Instead of extracting values from objects to process logic, it’s better to ask the object to process the logic itself.
So, by declaring an eval method in Expr, you can refactor as follows:
|
|