Comma operator = broken design

Jonathan M Davis jmdavisProg at gmx.com
Fri Dec 9 17:39:10 PST 2011


On Friday, December 09, 2011 20:18:36 Robert Jacques wrote:
> On Fri, 09 Dec 2011 01:20:45 -0500, Kagamin <spam at here.lot> wrote:
> > On Thursday, 8 December 2011 at 17:18:57 UTC, Joshua Reusch wrote:
> >>> Ahem. So are you suggesting that (a,b) means a tuple
> >>> everywhere but in a
> >>> for loop, where it is used to separate two statements?
> >> 
> >> If we use the comma operator only for tuples, there needn't to
> >> be a special case for loops:
> >> 
> >> for(x, y = 0 , 100; x < y ; x, y += 1,-1) { ... }
> > 
> > for(int x, y = 0 , 100; x < y ; x, y += 1,-1) { ... }
> > 
> > ?
> 
> Here's how I understood it:
> 
> int x,y; // Defined somewhere in the code above
> 
> for( (x,y) = (0,100); x < y ; (x,y) += (1,-1) ) { ... }
> 
> So you have variable capture, assignment and add-assign happening.

I'd _hate_ to be restricted to doing the same operation in the 3rd portion of 
a for loop for all of its parts. The comma operator is _perfect_ for there, 
and I wouldn't want to see its behavior changed there regardless. e.g. things 
like this should continue to be perfectly possible and legal

for(; cond; ++x, y *= 2) {}

Tuples do _not_ have the proper semantics for a for loop. It would be one 
thing for the comma operator were to go away in the general case, but it would 
be horrible IMHO for its behavior to be changed in for loops - be it in an 
effort to make it act like it's using tuples or any other reason.

- Jonathan M Davis


More information about the Digitalmars-d mailing list