3 ms·
are non-exhaustive pattern matches in kotlin warnings, errors, or neither? looks like from the docs: "If `when` is used as an expression, the `else` branch is
by platz 11y ago
are non-exhaustive pattern matches in kotlin warnings, errors, or neither?
looks like from the docs:
"If `when` is used as an expression, the `else` branch is mandatory, unless the compiler can prove that all possible cases are covered with branch conditions."
So it is an error, I assume?
- kcorbitt 11y agoWith the caveat that I haven't tried it myself: > What about pattern matching? This is where the word “sealed” above comes in handy; we can do an exhaustive pattern match without an else/otherwise branch from https://medium.com/@octskyward/kotlin-fp-3bf63a17d64a#.8dbwaz4yi https://medium.com/@octskyward/kotlin-fp-3bf63a17d64a#.8dbwa...
- commentzorro 11y agoThis looks to be single value matching only and no compile time exhaustion checks. This looks like really nice (i.e. sugary) alternative to switch but not quite pattern matching.
- udalov 11y agoIt's an error only when the 'when'-expression is used as a value, like assigned to a property, passed as an argument or returned from a function. Otherwise it's not an error. There's also a warning for matching enum values over an incomplete set of enum entries, but otherwise non-exhaustive 'when'-expression is green code. You can basically consider 'when'-expressions to be syntax sugar over Java's if-else or ternary operator (?:) chains.
- platz 11y ago> non-exhaustive pattern matches So there are no non-exhaustive pattern matches, is what you are saying? i.e. they all must be exhaustive. Except for your enum example
- udalov 11y agoNo, as I mentioned, only those 'when'-expressions, the result of which is used as a value, must be exhaustive. Those which are not, we call them 'when'-statements, correspond to an if-else chain in C-like languages and thus are not required to be exhaustive. More info in the official reference: https://kotlinlang.org/docs/reference/control-flow.html#when-expression https://kotlinlang.org/docs/reference/control-flow.html#when...