Bug 5432 – Allow variable declaration inside while condition
Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-01-08T06:31:00Z
Last change time
2011-08-27T20:43:25Z
Keywords
patch
Assigned to
nobody
Creator
eric.estievenart
Comments
Comment #0 by eric.estievenart — 2011-01-08T06:31:02Z
void f()
{
Object get() { return null; }
if( auto a = get() ) {} // OK
for( auto a = get(); a; a = get() ) {} // OK
while( auto a = get() ) {} // Does not compile
}
autowhile.d(7): expression expected, not 'auto'
autowhile.d(7): found 'a' when expecting ')'
autowhile.d(7): found '=' instead of statement
autowhile.d(8): unrecognized declaration
This is clearly inconsistent, so logged as a bug and not an enhancement.
Comment #1 by yebblies — 2011-08-27T09:17:45Z
*** Issue 6550 has been marked as a duplicate of this issue. ***
The for statement has a separate initialization and assignment. Initialization and assignment are very distinct operations, and the rewrites confuse the two. I'm not sure there even is a correct answer here. Is the variable created once, default initialized, and then assigned to each time, or is a new one created and initialized each time through the loop?
For the programmer who wants to declare a variable, a for loop does the job in a straightforward manner.