Replacing the comma operator (was: The empty statement "; " - when is it useful?)

Jarrett Billingsley jarrett.billingsley at gmail.com
Tue Jul 28 15:11:26 PDT 2009


On Tue, Jul 28, 2009 at 5:46 PM, Stewart Gordon<smjg_1998 at yahoo.com> wrote:
> Jarrett Billingsley wrote:
>>
>>  Tue, Jul 28, 2009 at 2:28 PM, Arthur Lloyd<via at google.com> wrote:
>>>
>>> Daniel Keep Wrote:
>>>
>>>> U comma(T,U)(T a, U b) { return b; }
>>>>
>>>> Is there any reason you couldn't use something like that?
>>>
>>> Sorry, this is probably a newbie question, but.. So instead of return a,
>>> b, c; you now have to write return comma(a, comma(b, c)); ? How will that
>>> help?
>>>
>>
>> typeof(T[$ - 1]) comma(T...)(T args) { return args[$ - 1]; }
>>
>> There.  Now you can do "comma(a, b, c)".
>
> Still won't solve the problem Andrei rightly pointed out.  But this will:
>
> T[$-1] comma(T...)(lazy T args) {
>    foreach (a; args[0..$-1]) cast(void) a;
>    return args[$-1];
> }

For one, that's almost verbatim what Tom S suggested in "comma
expressions must die."

For two, what problem did Andrei point out?  Order of evaluation of
function arguments?  On the contrary, he said "now that order of
evaluation will be defined to be left-to-right in D, the semantics
would be quite the same".  Your code is what you'd have to use in D1,
but in D2, the simpler version would suffice.



More information about the Digitalmars-d mailing list