[Issue 24675] New: Allow variable declarations in `for`conditions
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Tue Jul 23 14:17:42 UTC 2024
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 at puremagic.com
Reporter: qs.il.paperinik at 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++.
--
More information about the Digitalmars-d-bugs
mailing list