Bug 24675 – Allow variable declarations in `for`conditions

Status
NEW
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2024-07-23T14:17:42Z
Last change time
2024-12-13T19:36:37Z
Assigned to
No Owner
Creator
Bolpat
Moved to GitHub: dmd#20491 →

Comments

Comment #0 by qs.il.paperinik — 2024-07-23T14:17:42Z
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++.
Comment #1 by robert.schadek — 2024-12-13T19:36:37Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/20491 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB