-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Let us put braces in case labels with implicit break #1378
Comments
Allowing for the |
I'm not sure i get this part;
nothing seems ugly about either of those forms to me. |
The default indention formatting behavior of VS works nicely if you put switch (v)
{
case 1:
{
Foo();
break;
}
case 2:
{
Bar();
}
break;
}
|
It's funny, I was just about to propose this and I saw someone did just 9 hours ago, albeit the way the proposal is presented is awful. I personally dislike how scope is shared between cases in a switch. I always use brackets in every case to avoid scope being shared, which is consistent with how I always use brackets with That's my personal reasoning for it. It's not that big of a deal, and I don't know how much work it would be to add that feature, but if it's reasonable, then I don't think it would be inconsistent at all, especially if the "only if all cases do not have an explicit |
That's because there's no concept of 'fallthrough' in any other constructs. On the other hand, C# adopts 'switch' from its C heritage, and 'fallthrough' is absolutely a core part of that system. As such, C# wanted to be extremely explicit in the code to make it clear when fallthrough was happening, and when it was not. As such, fallthrough can only happen if you have: case A: // absolutely no code. only fallthrough
case B: Contrast that with 'break' (or some other flow construct) in all other cases, which clearly that fallthrough is not happening.
The problem with that is that then you can have: switch (expr) {
case a:
{
// stuff
}
case b:
{
// more stuff
}
} To anyone with a C 'switch' background (C, C++, Java, etc) this could be very confusing. It would very much look like to them that fallthrough was occurring. |
It would be inconsistent with how switch-without-breaks works for several other languages c# shares a lineage with. |
Is there actually a reason to continue this "C-style language compatibility" any further anymore? It made sense during C#'s first decade of existence when it was being adopted by the world, but now it's a mainstay language with it's own quirks and features. Okay, the syntax breaks syntax recognition, what about lambdas? Or even the fact that C# switches don't allow If this really is an issue, what if we added an optional syntax feature to |
The opposite must be asserted. Is there a good enough reason to move away from the decisions the language has made, and has served it well for all this time. Changes are expensive. They require a massive amount of effort both from within the team, and externally as well. All changes must justify themselves with the overall net benefit (which includes assessing the problems inherent with them). |
C# switches allow fallthrough. They allow implicit fallthrough when it is obvious and there's no chance of confusion. i.e. case A: // absolutely no code. only fallthrough
case B: THey also allow explicit fallthrough for the non-obvious cases. This gives the power and flexibility of the C-heritage switch-statements, while also addressing a major weakness of them (i.e. accidental implicit fallthrough in complex cases). |
You're welcome to propose such a thing. If someone on the LDM wants to champion it, it may go somewhere. However, in my personal opinion, there are a lot more important fish to fry. :) -- Here's my take on it: I personally don't see the current situation with switch/case/break is problematic enough to warrant a change here. What is far more important are issues like: "how do i use 'switch' in an expression context?" or "how do i find out if i'm not exhaustively switching?" or "how can i switch over things other than types/ints?". These issues are the one that actually dramatically effect how i can use 'switch' to solve problems. Whether or not i have to specify 'breaks' in my switch doesn't actually appreciably change the utility or power of switches, and so it doesn't really end up substantively impacting as much as other proposals would. |
I was ignoring the case of an empty case, in which case the implicit My primary concern was if this was too large of a change that the effort gone into it would outweigh the benefit. If you're suggesting I properly propose |
Sounds good to me. |
Only problem with current situation is scope sharing. No other code construct does that.
This proposal is not to change the power of switches. Implicit break is only for the case when we put braces. Existing syntax would not change in any way. The switch code snippet would remain same as it is now. The proposed syntax makes case |
Goto does that, and that's the most literally similar construct to a switch statement that I can think of. |
But everyone say |
@nvmkpk Nevertheless, it's not true that no other code construct does that. You can't ignore the context and history of the switch statement. |
Clearly, C# does not say that :)
It is used int the Roslyn codebase itself :) |
|
I guess it is fair to say that if you don't use |
Let's not debate about goto. The point here is about scope sharing in switch case. |
I'd like to have implicit |
For me must be significant reason to use |
@BannZay The new expression form of |
closing as duplicate of #3038 which is championed, and covers this and more |
I want to write something like:
We can do it right now but there are two issues in it:
case
statement.This also allows us to declare variables inside the brace without needing to expose them outside the
case
block.The text was updated successfully, but these errors were encountered: