Bug 6550 – Allow (auto var = expression) in while() loops
Status
RESOLVED
Resolution
DUPLICATE
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
All
Creation time
2011-08-24T12:42:00Z
Last change time
2011-08-27T09:17:45Z
Assigned to
nobody
Creator
andrej.mitrovich
Comments
Comment #0 by andrej.mitrovich — 2011-08-24T12:42:26Z
Currently this doesn't compile:
while (auto parent = foo.parent)
{
foo = parent;
}
Error: expression expected, not 'auto'
This is useful to replace this kind of loop (assume foo is a class object that has a 'parent' field):
while (true)
{
if (auto parent = foo.parent)
{
// do something with parent
foo = parent; // switch to next parent
}
else
{
break;
}
}
with the simpler:
while (auto parent = foo.parent)
{
// do something with parent
foo = parent; // switch to next parent
}
Currently `if` statements already allow this syntax, so it seems natural to extend this to `while` loops.
Comment #1 by timon.gehr — 2011-08-24T13:12:24Z
note that this can be implemented with a simple rewrite rule.
Comment #2 by yebblies — 2011-08-27T09:17:45Z
*** This issue has been marked as a duplicate of issue 5432 ***