-
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: return, break and continue expressions #14239
Comments
I am probably not used to expression syntax, but it looks a bit weird to me. I can understand how foreach(var item in list) {
var result = item match {
case 1: "one",
case 2: "two",
case 3: null,
default: null,
};
if (result == null) break;
else continue;
} I guess this is why C# team insists on keeping and "upgrading" the statement |
I think it could only work if there was some new |
Same applies to the Your alternative surely works for this particular example but in my opinion it's just a step away from being elegant. I didn't quite get your second comment. I don't think anything else is needed. For example you could just I also note that |
Consider a slight variation on your example: string result;
foreach(var item in list) {
result = item match {
case 1: "one",
case 2: "two",
case 3: break,
default: continue,
};
} What happens to |
@alrz wasn't this already answered though? #5143 (comment) |
Also, would any of the following be legal? Action x = () => break;
foreach (var i in new [] {1,2,3})
{
x();
}
x(); |
Ok, this answers it: https://users.rust-lang.org/t/matching-error-outside-of-loop-when-replace-return-with-continue/506 I guess this feature is a matter of taste. |
@DavidArno The same code rewritten with switch statement does produce an error as @dsaf You cannot use |
@alrz (PS) yeah I know, if only there was a way of embedding the posts instead of referencing. Updated the comment as per your wish. |
Yes there is. foreach(var a in b)
{
switch(a)
{
default: goto done;
}
}
done: |
@MgSam With |
Actually, in C#, the Quoting the language spec:
Of course, most people are irrationally afraid of |
The prototypical example in C# is breaking out of nested loops. foreach(var a in b)
{
foreach(var c in a)
{
if(c == foo) goto exit;
}
}
exit: Now try and tell me how that
In addition, I suspect you already use I digress, this is going off-topic from what this thread is about. |
You need to turn your "Now try and tell me how that
That's my point with "fearing"
Nope, it's because I've spend decades working with crap code (lots of it my own from the past) and |
The Java alternative in those cases being labeled loops and loops:
for (A a : b)
{
for(C c : a)
{
if(c == foo) break loops;
}
} Not that it has much bearing on this proposal, aside that even if C# were to adopt said syntax that it would also fit as an expression: int tally = 0;
loops:
foreach (var a in b) {
foreach (var c in a) {
tally += (c != foo) ? 1 : break loops;
}
} |
The Kotlin programming language has this feature and it turned out to be very valuable, especially in regards to null safety. Kotlin's val firstItem = list.firstOrNull() ?: return
for (e in listOfNullableValues) {
val nonNullableE = e ?: continue
} |
I agree with @cypressious . And they will increase the complexity of the code as they are nesting methods now. But thats just my point of view. |
@Gentlehag, Since witnessing the train wreck that the design team are making to variable scoping to satisfy their desire to be able to write: if (!TryX(out var result)) return;
// use result here I have become a big fan of @alrz's return, break and continue expressions proposal. The above could then be written as: var result = TryX(out var x) ? x : return;
// use result here The latter achieves the same result without causing scope leakage. I was originally sceptical of this idea, but the irresponsible way that the language is being broken to make Sadly though, @MadsTorgersen (and especially @CyrusNajmabadi) seem hell-bent on ignoring sage advice on the topic so I doubt this proposal will happen (and variable scope leakage will happen). 😞 |
@Gentlehag, Oh and for what it's worth, I 100% agree with you with regards to local functions too. We asked for implicitly typed lambdas and instead got given the nested method groups code smell. 😖 |
Nothing has been ignored. There is just a difference of opinion on the matter. :) Thanks for your contributions here. We always find them valuable and helpful when we discuss the matters internally! |
Difference of opinion:
Ignoring sage advice:
Spot the difference? |
I'm sorry we haven't been able to produce a solution you like :( . We've considered a lot of feedback (including many participants at our recent MVP summit), and we're more confident now that the set of trade-offs we've chosen are appropriate for the next version of the language.
You have been listened to, and you haven't been ignored. The language team discussed this issue at length and incorporated all our feedback avenues into the final design. Please don't mistake us deciding on something different as 'not listening' or 'ignoring'. We evaluated many possible designs and we weighed out the pros and cons. We brought in many voices into the discussion, and we evaluate the impact of our decisions on many styles of code. In the end, this imperfect solution won out over all the other imperfect solutions we were considering. :)
This is your perspective. Our own investigations on the topic has not indicated that this is the case. Again, i'm sorry that we weren't able to come up with a design that you like. We really do appreciate the feedback and will continue to use it when shaping future changes. Thanks! |
Now, if possible, can we get back on topic? Having a meta-discussion about how people feel when the LDM makes decisions they don't like isn't conducive to the topic at hand. Let's try to be respectful and accepting that not everything will go the way one always wants it to (lord knows that's how i've felt with every programming language a lot of the time :-)), and allow the discussion to focus on the future and not the past. Thanks! |
Really? That seems odd. @MadsTorgersen turned up and said "right, listen up, this is what we are doing", then later on rocked up and said "Scrap that, we are doing this instead". Then stormed up in a huff and said "La, la; la, la. I'm not listening. I've made my decision. Like it or lump it". So at what point did you feel sorry that this amazingly interactive process failed to result in a solution that people liked? |
Please keep the discussion on topic. If you want to talk more about your concerns here, you are welcome to reach me at cyrusn@microsoft.com. |
As far as I'm aware, we are discussing why @alrz's excellent proposal of allowing "return, break and continue expressions" couldn't be used as a way of avoiding variable scope leakage and thus is completely on topic IMO. However, I'm happy to defer to @alrz and will of course shut up and move on if he feels it is off-topic. And no, I absolutely will not contact you via email. Microsoft make great claims that C# is now "developed in the open". That means we discuss language design decisions in the open. Not at MVC conferences; not via email; but right here on GitHub. |
@DavidArno Discussing the topic is fine. Attempting to derail with comments like:
is not. Neither is:
Please, let's all be respectful, courteous, and professional here. Thanks!
We also discuss things over email, and any of the many mediums that allow one to converse without muddying up another topic. This is the last i'm going to say about this. Please keep the discussion on the issue at hand. That means actually discussing the language feature and not your feelings about how your feedback is being treated. If you want to discuss that elsewhere, i welcome that. It's just not appropriate here. |
I'm going to vouch +1 for at least return expression. It is quite useful for early breaks on nulls similar to throw expression. |
Moved to dotnet/csharplang#176 |
Since
throw
is now an expression, I thinkreturn
,break
andcontinue
are also good candidates to be an expression. Obviously,return
shouldn't be legal in expression bodied methods.There is no straightforward alternative for the third example as you will need to use the
switch
statement.The text was updated successfully, but these errors were encountered: