Should the comma operator be removed in D2?
Michael Rynn
michaelrynn at optusnet.com.au
Tue Nov 17 03:13:50 PST 2009
On Mon, 16 Nov 2009 18:57:13 -0600, Ellery Newcomer wrote:
> Bill Baxter wrote:
>>
>>
>> Note that if comma-as-sequencer was removed, and repurposed as a tuple
>> literal (with "a,b" making a tuple), and if we specify that tuple
>> elements are evaluated in order, then I think this would work as a
>> replacement for the current comma operator:
>>
>> (expr1,expr2,expr3)[$-1]
>>
>> But the nice thing is you can get the effect of a comma operator that
>> evaluates to the value of any expression in the sequence, just using
>> different indices.
>> Not sure how odious people would find forcing an order of eval on
>> tuples, though.
>
> I wouldn't mind.
>
> you'd also have to swap the precedence of assignment and comma, or
>
> a,b = b,a;
>
> isn't going to do anything useful. But then you can't have
>
> foo(a = b);
>
> unless the entire argument list is a tuple. But then you'd have to
> change {something or another} or you can't have nested tuples.
>
> backing up, assuming you don't allow unpareth'd tuples, then what is
>
> ( exp )
>
> ? Is it a tuple, or a scalar?
>
> I think my personal preference at the moment would be to have tuples of
> the form
>
> (, exp , exp ... )
>>
>> In the context of a for-loop, you don't care what the value is, so it
>> would work as a tuple as-is. The tuple itself would be the value:
>>
>> for(; i<10; ++i,++j) // just works
>
> for should be a special case
I read about it in the D Programming Language rough cut draft.
2.4.1 The Comma Operator
Expressions separated by commas are evaluated in sequence. The result of
the entire expression is the result of the rightmost expression. Example:
int a = 5;
int b = 10;20
int c = ( a = b, b = 7, 8);
After executing the snippet above, the values of a, b, and c are 10, 7,
and 8 respectively.
Thats all there is!
But the expressions may be as complex as you like?
Its syntactic sugar folks.
Nowhere says you absolutely need it to generate a particular compiled
code.
But where is the harmful example?
Some may want to remove it , in case it can be better used in a future
more useful role, or for enforcing source code readability standards.
But right now, its in the book!
--
michael *^&^*
More information about the Digitalmars-d
mailing list