comma operator causes hard to spot bugs

deadalnix deadalnix at gmail.com
Sun Apr 22 16:58:48 PDT 2012


Le 21/04/2012 20:25, Timon Gehr a écrit :
> On 04/21/2012 06:54 PM, bearophile wrote:
>> Jonathan M Davis:
>>
>>> There have been discussions about the comma operator before. I don't
>>> expect that it's going anywhere,
>>
>> Maybe there are intermediate solutions between keeping wild commas in D
>> and disallowing them fully. I think most of my bugs caused by commas are
>> similar to the one shown by the OP. This means this is not a common
>> source of bugs:
>>
>> foo(), bar();
>>
>> While this is sometimes a trap:
>>
>> auto x = foo(), bar();
>>
>
> This is not valid code.
>
>> So maybe it's enough to disallow using the last expression of a comma
>> sequence as result of the whole expression? I don't know. I almost never
>> use commas for such purposes. What are the use case for those commas?
>>
>> Bye,
>> bearophile
>
> if(r.front == 'a' && (r.popFront(), r.front) == 'b') { ... }
>
> There are a few similar usages in Phobos. If comma was to be used as a
> tuple constructor instead, those could be replaced by ( , )[$-1].

That is better.


More information about the Digitalmars-d mailing list