adam-mcneilly
09/25/2018, 6:18 PMwhen
statement support multiple conditions for the else block, does anyone know if there's an official feature request or discussion around this?
when (x) {
1, 2 -> println ("one or two")
3, else -> printlnt("doesn't compile")
}
kingsley
09/25/2018, 6:47 PM3,
Or rather (since this isn’t an assignment expression), you could replace else
with in 4..Int.MAX_VALUE
I’d most definitely go with the former thoughadam-mcneilly
09/25/2018, 6:51 PM3,
, you can argue that the next developer who comes through might assume I forgot to consider a case.arocnies
09/25/2018, 6:57 PMelse
case true duplication ("they change for the same reason at the same time"), or accidental duplication ("they change for different reasons at different times")?arocnies
09/25/2018, 6:58 PMkingsley
09/25/2018, 6:59 PMelse, 3 -> println("something")
should compile?themishkun
09/25/2018, 7:54 PMelse
case is basically true
, while ,
is ||
operator in boolean expression here. Why would anyone write boolean expression like this x == 3 || true
?adam-mcneilly
09/25/2018, 9:04 PMarocnies
09/25/2018, 9:06 PMDico
09/26/2018, 1:52 AMx == 3 || true
when they expressly indicate that the case 3
should do the same as else
block, despite it being effectively the same. It's just for code clarity, programmer knows it isn't a necessity.
Of course, the programmer can use a comment, but a comment can be anything. When compiled, the case can just be dropped and ignored like a comment, but that doesn't restrict the syntax or allow the programmer to move the case without thinking.
Of course, it's not a big nor important change, just giving my 3 cents.themishkun
09/26/2018, 7:55 AMwhen (x) {
1, 2 -> println ("one or two")
// 3 and other cases won't compile
else -> printlnt("doesn't compile")
}
Dico
09/26/2018, 7:57 AM3, else
.
I've mentioned comments as a potential workaround along with my thoughts on that.themishkun
09/26/2018, 7:57 AMDico
09/26/2018, 7:58 AMcase, else
is not adding a new feature.Dico
09/26/2018, 7:59 AMkingsley
09/26/2018, 8:01 AMDico
09/26/2018, 8:01 AMrobin
09/26/2018, 8:33 AMwhen (it) {
1 ->
2 ->
3 ->
4 ->
// case `1` is same as else-branch
else ->
}
And suddenly you're on your own to figure out if someone just forgot to delete the comment, or someone mistakenly added the 1-branch he thought was missing because he didn't read the statement to the end, or whatever. Documenting intent in a way that the compiler or at least the linter can help you with is always superior to comments, imo.robin
09/26/2018, 8:37 AMwhen (locale) {
DE ->
FR ->
EN, else ->
}
Which would very clearly document that the EN locale should be used if there is no specific instruction for the given locale. Of course you could do that mapping before the when statement, but then you might need to introduce a sealed class or an enum just to convince the compiler that the when statement is exhaustive, so it might be cleaner to just do it this way.arocnies
09/26/2018, 4:16 PMDico
09/26/2018, 4:19 PMrobin
09/26/2018, 4:22 PM