-
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
Proposal: infinite loop using while { ... }
without (true)
#2475
Comments
Note: Given that we already have Note: i am in a strange state of both liking this proposal, but also thinking it's so minor as to not really be worth it (even if free!). |
I find it confusing, honestly. |
A few quick thoughts:
|
'do' without 'while' is going to annoy someone who wants to write: do {
}
while (x) {
} :D |
I would logically expect this to |
Meh, I'm not a big fan. Infinite loops should not be so common that six characters represents a worthwhile saving. In fact, I don't think infinite loops should be more ergonomic. You should have to very explicitly declare your intent.
|
This seems like an IDE bug. I went and looked at the code here and it loos like we try to support this, but then it fails spectacularly. We legit check: // Semicolons in an empty for statement. i.e. for(;;)
if (previousParentKind == SyntaxKind.ForStatement
&& this.IsEmptyForStatement((ForStatementSyntax)previousToken.Parent))
{ So we know it's empty, but then we go and just do random stuff with the spacing. As if anyone would actually want |
There's no while-do loop :) It's a do-while loop. There should be no syntactic ambiguity here. |
The behavior has changed a few times. During a review of DotNetAnalyzers/StyleCopAnalyzers#633 we determined that the We intentionally chose |
Personally I don't like That said, for the times where it is necessary, this would be a welcome improvement.
I would say yes. Allowing an empty infinite statement ( |
I'd use it but I don't feel it's worth it. |
I have to disagree. The meaning of
Seeing an infinite loop makes me cry, too. 😛 |
is it? while{doSomething();} -- did i just forget to write a condition, or is this expected to continue until something exceptions?
I disagree, as while, without a condition is ambiguous, as you may sometimes never enter a while if the condition is false. If this proposal was for the condition on a DO loop to be optional, then i wouldn't have this disagreement.
SNR of being explicit is quite often worth it, for example long variable names are encouraged
Infinite loops are a pit of failure, i think we should make them be as infrequent as possible.
i don't think we would want to encourage this at a language level ...
does this go against your "It's immediately understandable" Pro above? |
Exactly. Hurray for those who accidentally forget to type the condition or accidentally remove it (say cut/paste instead of copy/paste) and end up with valid code that represents an infinite loop. This is one of those features that start at -100 points and immediately drop to -1000 for being unnecessary and dangerous at the same time. |
As far as I understand @gafter is hostile towards proposals that only save typing few characters e.g. dotnet/roslyn#8985 . |
Don't know whether it's worth mentioning but how about Just my 2c. :) |
I would think:
would just look like scoping, rather than it being a loop. Some languges have (do) blocks that don't imply looping. I know |
I agree that this may be too trivial to implement. If it is implemented, while I think semantically, https://github.com/tc39/proposal-do-expressions We already have something like that with the new |
@casperOne The |
+1 for loop {}. I could also go for repeat {}. -1 for reusing either do or while or for. |
I currently use for (;;) {} exclusively because I want to be able to find all if my infinite loops. The main utility of adding an infinite loop should be the ability to have analyzers reason better about the code, and to allow developers to search their code for them. Since developers implement infinite loops in many different ways, we have a strong anti-pattern. This isn't about saving keystrokes. Rather, since this use case is very specific and potentially dangerous it would pay dividends to have infinite loops as an explicit operator and to strongly discourage using the other loop flavors to implement infinite loops (sounds like a good opportunity for a code analyzer to address!). |
IMHO, |
I believe Rust has a keyword for an infinite loop, which is intentionally different from the conditional loops to help ensure the developer meant "loop forever" and didn't just forget to write a proper condition. loop {
// do things here, forever... or until a break is hit.
} ... um, yup. Here it is in their documentation.
|
To me a downvote means you disagree, but a confused reaction means I failed to explain something properly. Is there something anyone would like me to clarify? I'm not a fan of leaving issues hanging for a long time and the response is clearly mediocre, so I'm planning to close this issue soon. If the LDT thinks differently than the GitHub community, they can feel free to reopen. |
Yes, and to quote more from their documentation:
So now we have this Hmm, something's fishy about that. In my experience is that infinite loops are rare. In fact, true infinite loops are almost non-existing. In most cases when people use |
It seems quite obvious to me - you did not explain why is this necessary. I didn't leave a reaction but you can count me in the "confused" group. |
Pushing people to put the condition at the beginning or end of the loop results in extra complexity. This is the first reason from my initial post why
More reasons in the Pros section:
I think I remember others also asking for the |
But what's complex about it? The language has a construct that allows you to repeat a block of code while a condition is true. If you need to repeat forever then you simply use a condition that is always true - And even if it's complex, how often is such a loop needed? |
If
About 1:100 in the Roslyn codebase according to @CyrusNajmabadi. This feels like the right order of magnitude to me; I think the NUnit codebase and my own projects are around 1:100 as well. |
I never avoid writing As for linguistics, I don't see any meaning of "while" without a condition that makes sense: the loop certainly won't run for "a while", it'll run forever (if we don't do anything to prevent it), nor is it "whiling" away, as that implies that nothing is really happening (whereas most such loops in fact have a lot of very interesting things happening). As others have said, it reads more like "while what?" If you want your infinite loop to make complete linguistic sense, I agree |
I kind of disagree with this premise. While everything is certainly on a scale, I think that having a And if you disagree or find the "contortion" to be "torturous", you already have several ways to write these loops. |
These are good reasons and they give me a better sense of the community's mindset. Thanks all, I appreciate it! |
The current standard for conditionless loops is
while (true) { ... }
.An alternative is
for (;;)
, but various tools reformat it tofor (; ; )
and it looks like a hack.Both of these are awkward enough that we generally try to avoid ever writing them. Sometimes it's not avoidable. Other times, logic is repeated outside the loop or complexity is moved into the condition in order to get one more thing done before the exit condition when one or more statements before the exit condition would be more natural.
Would you consider allowing
(true)
to be dropped, leaving onlywhile {
to start the loop?Questions
Since there are no parentheses, does it seem best to require a block statement to follow it?
Should an empty statement be allowed?
Pros
Cons
Conditionless
while
is currently not as common as other forms of loops.If conditionless
while
becomes more common due to the pressure to stay away from it being gone, this could be seen as a bad thing. Maybe folks will reach for it too soon instead of first trying to structure their loop around the constraint of putting the condition first or last.(On the other hand, an IDE can easily detect and offer a fix.)
Rust has
loop { ... }
along withwhile cond { ... }
, so there's a chance thatwhile { ... }
is not as discoverable.The text was updated successfully, but these errors were encountered: