www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 24675] New: Allow variable declarations in `for`conditions

https://issues.dlang.org/show_bug.cgi?id=24675

          Issue ID: 24675
           Summary: Allow variable declarations in `for`conditions
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: qs.il.paperinik gmail.com

A `for` loop essentially is syntax sugar for a `while` loop, except that a
`while` loop can declare a variable in its condition, but a `for` loop can’t.

A variable declared in the condition can be mimicked:
```d
for (int i; int diff = 5 - i; ++i) { } // error
for ({int i; int diff; } (diff = 5 - i) == true; ++i) { }
```

Grammar change:
```diff
  ForStatement:
-     for ( Initialize Test? ; Increment? ) ScopeStatement
+     for ( Initialize IfCondition? ; Increment? ) ScopeStatement

- Test:
-     Expression
```

Not also that declarations in `for` conditions are allowed in C++.

--
Jul 23