-
Notifications
You must be signed in to change notification settings - Fork 4.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
[Proposal] make 'switch' more real-life usable #959
Comments
@XuTpuK Are you suggesting that we should make the "break" optional? |
@XuTpuK The issue of fall-through came up in another discussion; my comment there gives an example of the current C# syntax for fall-through. My interpretation of the overall request is the same as @gafter (a request to make For background, you may also be interested in the following two very informative comments from Eric Lippert. They explain the details of the current syntax as well as the original reasoning for requiring a |
@sharwell I don't think "break" is needed at all - that's the point! EVERY "case" should be implicitly finished with jump to the end of "switch". |
@XuTpuK Just to make sure I understand, you'd prefer that if a programmer writes switch (i)
{
case 1:
// no statements
case 2:
Console.WriteLine("two");
} Then this should print |
i need a downvote here ;P |
@gafter Nope. Both cases (1 & 2) are labels for one statement block. |
For some reason I thought this was already possible, but then I tried it real quick and apparently it's not. (Weirdly enough, it says |
@XuTpuK The rationale used during the original design which included the break statement is part of the first link I posted. Here is the specific excerpt:
This doesn't mean it can't change, but it's still a relevant point in favor of including an explicit @Joe4evr The error with the code is due to missing a |
@sharwell Oh, derp. So that part about using multiple case labels for a single statement block is already possible just fine. Then yeah, there is no added value to this proposal other than attempting to make |
It's probably not my place to say this, but can we enforce some sort of etiquette on here? I've noticed this is not XuTpuK's first case where he/she is using an extremely confrontational tone. Telling other users that they need a "brain" and patronizing them as beginners surely does not foster a good discussion environment. |
I think the rationale for making break obligatory is excellent. It's not a useful feature, and when you need fall-through it's very easy to make it work anyway. If the switch needed any rework it would be to make it act more like expressions i.e. that they can return a value, and look more like classes instead of a goto-list. |
@chrisaut +1 I absolutely agree that break should be non-optional. In my opinion it's worth to make switching easier/shorter but through functional-style pattern-matching constructs, which don't have "follow-up" issues because they always end with an implicit "return". But there is already an active discussion about pattern-matching style constructs in #206 . |
@XuTpuK I can't argue with you about my brain, thinking about it reminds me of the Halting Problem. |
@XuTpuK We of small brains need things explained using small words. Please be patient and help us become more than beginners. |
The fallthrough is lost is C# because compilers are free to reorder switch cases. That's also why, if we need to continue on another case, we need to explicitly jump ( Not having a an explicit If I were to equate brain sizes to proposals... never mind. |
@sharwell I see absolutely nothing what explains why we need 'break'. Did you read carefully my proposal? I explained clearly that "FALL THROUGH" is the least necessary thing we need from a simple SWITCH operator. If you repeat something after "well known Lippert" it doesn't make his useless words more meaningful, huh? Just read again proposal and try to explain by yourself WHY we need break. I see no any reason except compatibility with legacy ... java? C? |
|
@paulomorgado There is nothing about "readability". Let's extend your logic on 'if' operator:
So what? Where is readability - I don't see. As well as I don't see ANY reason stating "oh, this 'case' branch is finished, shouldn't we write 10 more lines to say it clear?!". |
@XuTpuK You claim that the only point of a |
@XuTpuK please keep the comments and feedback professional. We will not tolerate abusive conversations on this repo. |
@sharwell First of all, let's state - we programming on C#. And this language evolves. What's the deal to look at legacy outdated languages like C++/C/Java? Why you don't worry about "6 spaces indent" in Fortran? Just imagine that shock when fortran-guy come and see there is characters before 6-th position!! Same ridiculous is your worry about C++ guys. And hell, dude, C# exist TWELVE YEARS, don't you think it's more than enough to stop worrying about "newcomers from C"?? C# already got its niche and rewards full respect as a primary language for Windows development. I hope my arguments are more than clear. |
|
While the design of the If we were to change the language to say that there is an implicit Having said that, we are considering an alternative switch construct for use with the pattern-matching proposal. The main motivation would be to provide an expression form. Since it would be a distinct syntactic form, we are not constrained by the sensibilities (or otherwise) of the existing switch statement. Thanks for the question. I hope we've answered it to your satisfaction. |
@gafter You understand it wrong. There is NO breaking change:
Even if some "mattern-matching switch" will be introduced, it doesn't make life easier with current "switch". |
@XuTpuK, your earlier comment was deleted due its rude / offensive nature. Please keep the discussion on this topic professional. If you continue to post comments of this nature we will have to consider more serious actions like banning. |
@XuTpuK I understand precisely. You want two labels with no statement between them to behave fundamentally differently at runtime than two labels with an empty statement between them. That is why I said
It was because I understood that what you want is too subtle for code readability that I closed this issue. |
@gafter Nope, you said I offer "breaking changes". Now you changed your arguments, but they again are inconsistent, because in 99.9999% cases we DO NOT need "empty operator"! Readability (and reliability) will increase since we don't dirt code with unnecessary "breaks" and always make sure we have exactly one branch to execute. Shortly, I ask for a syntactic sugar to skip UNNECESSARY operator "break" in places where it's obvious as 2x2. |
Readability will certainly not increase, ";" is way too easy to overlook. C# is not a language for code golf where every character needs to go away. Clear, unambiguous code is way more important. Are you seriously saying that difference between switch(a) {
case 1:
case 2:
do_smth();
} and switch(a) {
case 1: ;
case 2:
do_smth();
} is easy to spot at a glance? gafter did not say you offer breaking changes, he said doing implicit breaks in all cases would be a breaking change (which it is), and doing it your way with a small ";" differentiating the implicit break case from the fall through case would be too subtle. I suspect you are simply too smart for even the smartest people working on Roslyn to understand. I encourage you to create your own programming language so you don't have to waste your "priceless" time with discussions with us peasants who clearly don't understand. |
@chrisaut See my comment above and appropriate percentage. I don't understand what fun you found in issuing arguments which are nothing, but theoretical ideas behind the beer. From practical POV nobody will need to "skip one value" - most of the cases are "run one of many branches". From this POV we just waste characters to explain compiler what he already knows. Why? Just because somebody's knowledge stoned in 70-es? Then why we meet here SUPPOSING to improve the language?
SHOW me these cases! I didn't see any. |
BTW for people who forgot history: switch was introduced as a syntax sugar for constructions like:
As you see, not even mention of "fall through" or jump to the case label - these hacks was introduced in C, because C itself is a "high level assembler", which allows any crazy tricks. C# is a high level language, trying to save programmer from mistakes. One of such solutions is prohibition of "fall through" - THAT is why all these breaks become outdated - compiler already assume you always finish branch with some jump. And most used jump is break which of course can be safely skipped. |
He means if the "break" were implicit in all cases, then it would be a breaking change. This is not your suggestion. It was just a general remark regarding implicit breaks. Of course @gafter himself can confirm if that is indeed what he meant, I don't want to speak for him. The breaking change is not the argument against this, it's an argument against a different proposal that just came up in this thread and hence was mentioned. The argument against this is that it is way too easy to overlook and thus hurts readability for the only benefit to save a few keystrokes. The general consensus seems to be that this is not a worthy tradeoff. You may of course disagree. People absolutely need to just skip one value, in fact a lot of switch statements around enums do just that. Not most of the time, but it def. does happen. I don't know where you got 99.9999% percent from, I assume you ran an analysis against thousands of solutions? If true it would make a strong argument for your case. Please do share that data. |
While I really hate XuTpuK's attitude, I tend to agree with him in one switch(a) { It should be perfectly fine to explicitly put break there if one needs switch(a) { case 2: It needs to be allowed for backward compatibility reasons. However, when I tend to see "break" in switch statements the same way as "return" in the 2015-03-04 11:55 GMT+01:00 XuTpuK notifications@github.com:
|
@XuTpuK It seems you forgot history. Switch wasn't introduced in C. It already existed in B, and B had more or less taken it directly from BCPL's edit: sorry this is completely off-topic and does not belong here at all. |
@Przemyslaw-W I hate myself, thanks for support. :)
Exactly! While both, we and compiler, know that "case" is finished, we still forced to say "OK, I'm done!". WHY?? |
I have an idea about this:
|
What's the real benefit over this:
|
More consistent with the rest of the language.
|
Take for example the following snippet: The compiler is complaining about missing break statement, thus break is required, thus only one way is supported and thus there is NO AMBIGUITY about symantical meaning, however this syntax is not supported. I think this is a discussion about style, do we want to be explicit or implicit about break? implicit break could work very fine, however we have to consider if this is not confusing when we already have implicit fall through in case body is empty. |
What I proposed still allows to use the current style (with breaks) but also adds a new {brackets} style which, yes, is more consistent with the rest of the language and doesn't need "break". @ibrahimbensalah I didn't ask to make break implicit. It's still required when using "case ... :" style (but not with "case (...) { ... }"). |
@AqlaSolutions I like that. It does feel more inline with the rest of the language like that. If we assume ever
Why not indeed. It is one of the features of Go that I'm most envious of. |
See the following issues: |
'switch' operator was blindly copied from C/Java, but 1) by some strange reason lost ability to fall from one branch to another 2) still unusable like in original languages
What is "nature" of 'switch'? SWITCH code branches, running exactly one of 'em! So if I declare branch "case 1: do_that", why the hell I should worry about all that "break"s, "fall through" and so on?!
Switch should BE the switch:
That's it! Why we like monkeys still repeat invention of 70-th "case 1: .... break;" if we even cannot fall through?! Good question to designers of C#!
The text was updated successfully, but these errors were encountered: