[Issue 6550] New: Allow (auto var = expression) in while() loops

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Aug 24 12:42:28 PDT 2011


http://d.puremagic.com/issues/show_bug.cgi?id=6550

           Summary: Allow (auto var = expression) in while() loops
           Product: D
           Version: D2
          Platform: Other
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: andrej.mitrovich at gmail.com


--- Comment #0 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 2011-08-24 12:42:26 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list