D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6550 - Allow (auto var = expression) in while() loops
Summary: Allow (auto var = expression) in while() loops
Status: RESOLVED DUPLICATE of issue 5432
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other All
: P2 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-08-24 12:42 UTC by Andrej Mitrovic
Modified: 2011-08-27 09:17 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrej Mitrovic 2011-08-24 12:42:26 UTC
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 timon.gehr 2011-08-24 13:12:24 UTC
note that this can be implemented with a simple rewrite rule.
Comment 2 yebblies 2011-08-27 09:17:45 UTC

*** This issue has been marked as a duplicate of issue 5432 ***