https://kotlinlang.org logo
Title
k

karelpeeters

09/25/2018, 7:17 AM
Well it's not useful to do this,
3, else
is just
else
right?
r

robin

09/25/2018, 7:33 AM
I'd say it could be useful to document developer intent in some cases, for example to show that a case that seems to be missing is in fact not, it just happens to need the same execution path as the else case.
👍 3
k

Konstantin Petrukhnov

09/25/2018, 9:08 AM
Why not putting intent as a comment?
r

robin

09/25/2018, 9:15 AM
Because then we will definitely at some point end up with stuff like this:
when (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.
d

Daniele Segato

09/25/2018, 9:54 AM
I agree with Robin. That was my point. don't see why I can't use else with the comma there
The example is stupid. but I actually have a bit of code in there, and having to export in a function or repeat it just cause the language doesn't just let me use
<condition>,else
is meh
k

karelpeeters

09/25/2018, 9:56 AM
You don't need to repeat code! You can argue for documentation purposes, sure, but not repitition.
d

Daniele Segato

09/25/2018, 9:56 AM
how can I avoid repeating code?
k

karelpeeters

09/25/2018, 9:57 AM
Replace
3, else
with
else
.
d

Daniele Segato

09/25/2018, 9:58 AM
when(t) {
    1 -> "A"
    2 -> "B"
    // 3,
    else -> "C"
}
like this? this sucks honestly I prefer
when {
    t == 1 -> "A"
    t == 2 -> "B"
    t == 3,
    else -> "C"
}
at this point
my point is I see no reason for the language to forbid that
a

adam-mcneilly

09/25/2018, 2:56 PM
The language isn't forbidding anything. You are able to achieve the behavior you want with just
else
. The opinionated part of this is: If 3 and "everything else" do the same thing, you don't have any reason to specify 3 as its own case, do you?
r

robin

09/25/2018, 3:05 PM
I literally just stated "documentation of intent" as a valid reason to specify 3 as its own case. So yes, I do have a reason. We're not writing code for computers, we're writing it for other humans as well, so just behavior is not all a language is meant to specify.
And yes, it's technically not "forbidding" it, the possibility of doing it is just not included in the design of this part of the language. Not sure how that matters, it's not possible, and we're talking about whether it might be a good idea to make it possible.
a

adam-mcneilly

09/25/2018, 3:29 PM
Has anyone seen a youtrack feature for this or JB discussion somewhere? I'm having trouble finding it.
r

robin

09/25/2018, 3:37 PM
No idea, we were just discussing it in this thread so far. Could move this to #language-proposals or just open a feature on youtrack if there's not one yet.
👍 1
a

adam-mcneilly

09/25/2018, 3:40 PM
I can't imagine there isn't one. I asked this questions months ago too and got the same response. I ended up just growing into it honestly, but I think it's important for there to be a more formal spot for people to say whether or not they want this.
We can start there IMHO maybe someone who's a regular in that channel knows of one.
d

Daniele Segato

09/25/2018, 5:04 PM
can you post link here if you create it?
also how come I couldn't see #language-proposal until I clicked the link? (not very expert of slack, is there a way to discover other channels?)
i

ilya.gorbunov

09/25/2018, 6:02 PM
@Daniele Segato Just click on "Channels" in the left side bar.