|
| 1 | +# `when` expressions |
| 2 | + |
| 3 | +```kotlin |
| 4 | +object Bob { |
| 5 | + fun hey(statement: String): String = |
| 6 | + when { |
| 7 | + statement.isQuestion() && statement.isYelling() -> "Calm down, I know what I'm doing!" |
| 8 | + statement.isQuestion() -> "Sure." |
| 9 | + statement.isYelling() -> "Whoa, chill out!" |
| 10 | + statement.isSilence() -> "Fine. Be that way!" |
| 11 | + else -> "Whatever." |
| 12 | + } |
| 13 | + |
| 14 | + private fun String.isSilence(): Boolean = this.isBlank() |
| 15 | + private fun String.isQuestion(): Boolean = this.trim().endsWith('?') |
| 16 | + private fun String.isYelling(): Boolean = any(Char::isLetter) && toUpperCase() == this |
| 17 | +} |
| 18 | +``` |
| 19 | + |
| 20 | +In this approach you have a `when` expression containing different so-called branches that can be matched using the corresponding patterns. |
| 21 | +As soon as one of these patterns on the left side of the arrow (`->`) is matched ... |
| 22 | + |
| 23 | +1. the value on the right side |
| 24 | +2. the block on the right side is executed and the value retuned from the block |
| 25 | + |
| 26 | +... is returned from the `when` expression. |
| 27 | + |
| 28 | +Only one branch is matched in one execution of the `when` expression. The branches are matched from top to bottom and the first branch in which the condition evaluates to `true` is selected. |
| 29 | +If none of the given conditions matches the `else` branch is selected and returned. |
| 30 | + |
| 31 | +~~~~exercism/caution |
| 32 | +Depending on what patterns are on the left side of your branches the order of branches is important to the correct execution of the `when` expression since the first matching branch is selected. |
| 33 | +~~~~ |
| 34 | + |
| 35 | +An [object declaration][object] is used to define `Bob` as essentially a [singleton][singleton] object instantiation of the class. |
| 36 | +This is sufficient, since there is no object state that needs to change with each call of the `hey` method. |
| 37 | + |
| 38 | +Inside this object there are some `private` [extension methods as members][extension-members] of the `object`. This adds these methods to the `String` data type for `private` usage of these methods in this `object`. This allows calling the methods directly on the `String` instead of passing the `String` to the method. |
| 39 | +(More about extension methods in general [here][extension-general]) |
| 40 | + |
| 41 | +These extension methods check for: |
| 42 | + |
| 43 | +1. `isSilence()`: a blank string |
| 44 | +2. `isQuestion()`: a string with the last non-whitespace-character being a question mark |
| 45 | +3. `isYelling()`: a string with all letters in uppercase |
| 46 | + |
| 47 | +When combining these methods as in the `when` expression above you can map all the cases required by the exercise. |
| 48 | + |
| 49 | + |
| 50 | +[when]: https://kotlinlang.org/docs/control-flow.html#when-expression |
| 51 | +[object]: https://kotlinlang.org/docs/object-declarations.html#object-declarations-overview |
| 52 | +[singleton]: https://en.wikipedia.org/wiki/Singleton_pattern |
| 53 | +[extension-members]: https://kotlinlang.org/docs/extensions.html#declaring-extensions-as-members |
| 54 | +[extension-general]: https://kotlinlang.org/docs/extensions.html |
0 commit comments