Add when concept - #742
Conversation
| # Introduction | ||
|
|
||
| Kotlin's `when` is a conditional expression for choosing among several options. | ||
| It is similar to `switch` in Java, but more flexible than a chain of `if` ... `else if` ... `else`. |
There was a problem hiding this comment.
| It is similar to `switch` in Java, but more flexible than a chain of `if` ... `else if` ... `else`. | |
| It is similar to `switch` in Java, and more flexible than a chain of `if` ... `else if` ... `else`. |
| - Each branch is written as `pattern -> result`. | ||
| - Branches are tried from to bottom until a match is found. Only the first branch runs. | ||
| - Use `else` for any value not listed above (similar to the final `else` in an `if` chain). | ||
|
|
There was a problem hiding this comment.
So this is pattern matching then?
Could be something to call out!
There was a problem hiding this comment.
Added mention about pattern matching.
| val x = 5 | ||
| val y = 10 | ||
| val description = when (x) { | ||
| 5 if y % 2 == 0 -> "x is 5, y is even" |
There was a problem hiding this comment.
Yeah, regexp can work as part of the guard. Something like this:
val code = 3
val classification = "animal"
val type = when (code) {
3 if """.*mal""".toRegex().matches(classification) -> "animal"
else -> "unknown"
}But if using it match the value, I think you can only do it by matching conditions (i.e. without a value to match). For example:
val classification = "animal"
val type = when {
Regex("''.*mal""").matches(classification) -> "animal"
Regex(''"fru.*""").matches(classification) -> "fruit"
else -> "unknown"
}There was a problem hiding this comment.
Oh, I just learn I can indeed use Regexp but would need to define an extension function:
operator fun Regex.contains(text: CharSequence): Boolean = this.matches(text)
fun main() {
val classification = "animal"
val res = when(classification) {
in Regex(""".*mal""") -> println("animal")
in Regex("""fru.*""") -> println("fruit")
else -> println("unknown")
}
println(res)
}There was a problem hiding this comment.
Added something about this in the about.md.
| # Introduction | ||
|
|
||
| Kotlin's `when` is a conditional expression for choosing among several options. | ||
| It is similar to `switch` in Java, but more flexible than a chain of `if` ... `else if` ... `else`. |
There was a problem hiding this comment.
| It is similar to `switch` in Java, but more flexible than a chain of `if` ... `else if` ... `else`. | |
| It is similar to `switch` in Java, and more flexible than a chain of `if` ... `else if` ... `else`. |
| These are the rules for the different replies: | ||
|
|
||
| - If the guess is `42`: "Correct" | ||
| - If the guess is `41` or `43` and there have been less than : "So close" |
There was a problem hiding this comment.
| - If the guess is `41` or `43` and there have been less than : "So close" | |
| - If the guess is `41` or `43` and there have been no more than 5 guesses: "So close" |
| # Introduction | ||
|
|
||
| Kotlin's `when` is a conditional expression for choosing among several options. | ||
| It is similar to `switch` in Java, but more flexible than a chain of `if` ... `else if` ... `else`. |
There was a problem hiding this comment.
| It is similar to `switch` in Java, but more flexible than a chain of `if` ... `else if` ... `else`. | |
| It is similar to `switch` in Java, and more flexible than a chain of `if` ... `else if` ... `else`. |
This adds the
whenconcept for Kotlin. Also took the opportunity to fix the name for the "Log Levels" exercise.@SleeplessByte, this is ready for review