Proposal: add syntax for making a loop without included conditions/statements #8213
Replies: 3 comments
-
Beta Was this translation helpful? Give feedback.
-
There is technically: using System;
public class C
{
public void M()
{
Loop:
Console.WriteLine("Are we there yet?");
goto Loop;
}
} |
Beta Was this translation helpful? Give feedback.
-
While I agree that, if we were building a new language, it might be a good idea to add a Rust has a loop keyword because it enables scenarios that would be otherwise disallowed by the type system (regarding lifetimes, definite analysis, Aside: Rust also allows one to let mut i = 1;
let something = loop {
i *= 2;
if i > 100 {
break i;
}
};
assert_eq!(something, 128); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently, there is no way in C# to specify a "plain" loop without conditionals (
while
,do…while
) or statements (for
). My proposal is to add support for such a loop to C#.Justifications
There are a number of scenarios where being able to declare a "plain" loop such as this could actually be useful, including cases where we could avoid unnecessary additional evaluations of the same conditional statements, or handle more complex logic flows around the conditions when we should exit the loop. Some example cases include:
while
is reached)Of course, there are ways to get this behavior with the existing loops in C# by specifying
(true)
forwhile
/do…while
or afor
loop with empty statements (just;;
), but these all are "hack-y" and hurt readability due to the extraneous code and breaking convention with the intended usage of these loops. Being able to simply have a loop with syntax that doesn't require conditional/statement in the first place easily avoids any confusion from these incongruent usages.Syntax
I am of course open to discussion on what the syntax should be to implement this, but I would propose adding this to C# syntax either as a
do
loop without a while, or just simply using the wordloop
. Using while without a condition (as I have seen proposed before) is confusing, since the word while itself implies a condition ("while? while what?"). Using ado
without a while makes much more sense semantically and would fit well with established C# naming conventions, although I believe we would still have to require a terminating;
at the end (as ado…while
loop already requires anyway) to avoid ambiguity in the event a separate while loop was declared after it. And using just the wordloop
would also work well as using a different keyword avoids any confusion with existing loops. If there is concern perhaps that loops intended to have an exit condition by design could now lack that, simple checks could be added that aloop
ordo
without awhile
that lacks any break statement in the body automatically triggers a warning, and then we don't have to worry about an uptick in programmers accidentally writing infinite loops because we introduced a kind of loop that doesn't require exit conditions/statements.Beta Was this translation helpful? Give feedback.
All reactions