rob42
08/02/2024, 10:08 AMrob42
08/02/2024, 10:09 AMJoffrey
08/02/2024, 10:10 AMPair<Boolean, Boolean>
type is kinda like Pair<T, U>
, and is not supported for this.
You'll get exhaustiveness for enums and sealed classes, mostly.rob42
08/02/2024, 10:13 AMrob42
08/02/2024, 10:32 AMJoffrey
08/02/2024, 10:33 AMrob42
08/02/2024, 10:48 AMswitch (isSuccess, isRemote) {
case (true, true): return "Great! Your request was successful"
case (true, false): return "Great! Your remote request was successful"
case (false, true): return "Oh no, your request failed"
case (false, false): return "Oh no, your remote request failed"
}
Now there are of course many ways you could generate this string, but this kind of approach is extremely clear and avoids awkward string interpolationKlitos Kyriacou
08/02/2024, 10:56 AMPair
class that the library defines.
I've often got around this by using nested if/else
or by adding else -> error("Impossible")
, neither of which look good.Riccardo Lippolis
08/02/2024, 11:05 AMval baz = when {
isFoo && isBar -> "Lorem"
!isFoo && isBar -> "Ipsum"
isFoo && !isBar -> "Dolor"
!isFoo && !isBar -> "Sit"
}
However, it gives the 'when' expression must be exhaustive
error on the when
, AND it gives a warning that the last branch (!isFoo && !isBar
) is always true 😅 . Replacing the last branch with else
fixes both the error and the warning...rob42
08/02/2024, 11:31 AMCAB
08/02/2024, 12:25 PMelse
makes most sense to me. Add the comment at that line that it is the only remaning case.ephemient
08/02/2024, 3:08 PM