Well it's not useful to do this, `3, else` is just...
# getting-started
k
Well it's not useful to do this,
3, else
is just
else
right?
r
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
Why not putting intent as a comment?
r
Because then we will definitely at some point end up with stuff like this:
Copy code
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
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
You don't need to repeat code! You can argue for documentation purposes, sure, but not repitition.
d
how can I avoid repeating code?
k
Replace
3, else
with
else
.
d
Copy code
when(t) {
    1 -> "A"
    2 -> "B"
    // 3,
    else -> "C"
}
like this? this sucks honestly I prefer
Copy code
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
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
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
Has anyone seen a youtrack feature for this or JB discussion somewhere? I'm having trouble finding it.
r
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
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
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
@Daniele Segato Just click on "Channels" in the left side bar.