Comma operator = broken design

Robert Jacques sandford at jhu.edu
Sun Dec 11 17:19:11 PST 2011


On Sun, 11 Dec 2011 06:21:51 -0500, Alex Rønne Petersen <xtzgzorex at gmail.com> wrote:

> On 10-12-2011 02:39, Jonathan M Davis wrote:
>> 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
>
> Indeed, having the comma operator in for loops is perfectly normal; even
> C# has this.
>
> - Alex
>

A lot (all?) of for loop use cases seem to actually work in tuple form. For instance,

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

Will behave as expected, although there might be a performance issue.


More information about the Digitalmars-d mailing list