D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5432 - Allow variable declaration inside while condition
Summary: Allow variable declaration inside while condition
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords: patch
: 6550 (view as issue list)
Depends on:
Blocks:
 
Reported: 2011-01-08 06:31 UTC by Éric Estièvenart
Modified: 2011-08-27 20:43 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Éric Estièvenart 2011-01-08 06:31:02 UTC
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 yebblies 2011-08-27 09:17:45 UTC
*** Issue 6550 has been marked as a duplicate of this issue. ***
Comment 3 Walter Bright 2011-08-27 20:43:25 UTC
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.