Skip to content

Instantly share code, notes, and snippets.

@elizarov
Last active October 11, 2018 11:56
Show Gist options
  • Save elizarov/c283faf16bb3ec011a6366fd32a9e624 to your computer and use it in GitHub Desktop.
Save elizarov/c283faf16bb3ec011a6366fd32a9e624 to your computer and use it in GitHub Desktop.
class Person(val isClever: Boolean)
val p = Person(isClever = true)
// ERROR - missed "false"
val example1 = when (p.isClever) {
true -> "Yes, this person is clever"
}
// No error - all cases matched
val example2 = when (p.isClever) {
true -> "Yes, this person is clever"
false -> "Fully matched!"
}
// ERROR - even though all cases are "theoretically" matched
// replace "number <= 10" with "else" to get it to compile
val example3 = run {
val number = 10
when (p.isClever) {
true -> "Yes, this person is clever"
// here is how you structure it
false -> when {
number > 10 -> "Clever when number is more than 10"
number <= 10 -> "Clever when number is not more than 10"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment