Comma operator = broken design

Timon Gehr timon.gehr at gmx.ch
Fri Dec 9 03:39:55 PST 2011


On 12/09/2011 10:19 AM, Don wrote:
> On 08.12.2011 20:22, Timon Gehr wrote:
>> On 12/08/2011 05:02 PM, Regan Heath wrote:
>>> On Thu, 08 Dec 2011 12:17:20 -0000, so <so at so.so> wrote:
>>>
>>>> On Thu, 08 Dec 2011 11:17:48 +0200, Dejan Lekic
>>>> <dejan.lekic at gmail.com> wrote:
>>>>
>>>>>
>>>>>>
>>>>>> Why is this operator still kept around?
>>>>>
>>>>> No offense, but I find it strange/funny that you even ask why! :)
>>>>>
>>>>> Have you never used comma in for loops???
>>>>>
>>>>
>>>> Not sure if it is that relevant for D, but good point.
>>>>
>>>> for(auto i=beg(), e=end(); i!=e; ++i)
>>>> for(auto i, j, k;; ++i, ++k)
>>>> ...
>>>
>>> It's kinda amusing that this thread appeared just as we had a case of
>>> this here at work. The developer accidentally coded something like...
>>>
>>> if (function(..), FALSE)
>>> {
>>> }
>>>
>>> Accidentally adding the ", FALSE" /after/ the ) instead of as a new
>>> parameter to the function.
>>>
>>> (note; This was in C++ with default parameter values so the function can
>>> in fact take 1-4 args).
>>>
>>> When I pointed this out, he said "how does that even compile" and was
>>> completely unaware of the existence of the comma operator, nor (once I
>>> explained it) did he realise it was in any way related to the comma used
>>> in for loops. People simply don't think of them as being the same thing
>>> at all. Instead, people learn the comma syntax as a special
>>> characteristic of the for loop, and use it nowhere else.
>>>
>>> I think the comma operator is of little benefit (except where used in a
>>> for loop) and it is a source of bugs and we'd be better off without it.
>>> Even if it means porting C/C++ requires modification, or existing D (I
>>> doubt very much of it) breaks.
>>>
>>
>> Phobos would break, for example. And some of my code too.
>
> Are there any cases where you're using comma outside of for loops?

Yes, one for every 65 LOC.

> I wonder how much would break if were made illegal everywhere else.

These are the occurences of the comma operator in directory 'std':

return r2.empty ? (r1 = r, true) : false;
return binaryFun!pred(r.front, e) ? (r.popFront(), true) : false;

if (f.flPlus)
     signChar = '+', ++minw;
else if (f.flSpace)
     signChar = ' ', ++minw;

if (std.ascii.toLower(p.front) == 'n' &&
        (p.popFront(), std.ascii.toLower(p.front) == 'f') &&
        (p.popFront(), p.empty))

enforce((p.popFront(), !p.empty && std.ascii.toUpper(p.front) == 'A')
         && (p.popFront(), !p.empty && std.ascii.toUpper(p.front) == 'N'),
        new ConvException("error converting input to floating point"));

if (indexStart != 0)
     formatValue(w, indexStart, f), put(w, '$');

if (c == '\"' || c == '\\')
     put(w, '\\'), put(w, c);
else
     put(w, c);

return (++mi.m_cRefs, cast(HXModule)mi);
return (++mi.m_cRefs, hModule);

return
     c <= 0x7F ? 1
     : c <= 0x7FF ? 2
     : c <= 0xFFFF ? 3
     : c <= 0x10FFFF ? 4
     : (assert(false), 6);







More information about the Digitalmars-d mailing list