Comma expressions must die [Was: Reddit: why aren't people using D?]

Adam D. Ruppe destructionator at gmail.com
Thu Jul 23 18:22:03 PDT 2009


On Thu, Jul 23, 2009 at 11:49:29PM +0100, Tom S wrote:
> It's not much worse, but it's not everything that's to tuples. Here's 
> one more example:
> 
> > int a, b;
> 
> ----
> 
> > a, b = 2, 3;


Why would you ever want to do that when you have:

a = 2;
b = 3;


I see people ask why would you ever want the existing comma operator, when
you can just separate it into two lines, and the replacement use could also
just be separated into two lines.

The swap function I've seen tossed around could just be swap(x,y) instead of
y,x = x,y too. A fancier rearrange could be done with a template, so:

(x, y, z) = (z, y, z)

becomes:

rearrange(x, y, z,   z, y, x);




I could see a function returning it as being potentially useful for the
x,y case:

Tuple!(int, int) getPoint() { return tuple(x, y); }
...

x, y = getPoint();


But, a better way to do this would be:

Tuple!(int, "x", int, "y") getPoint() { return tuple(x, y); }
...

auto p = getPoint();
// work with p.x and p.y

It is two brief lines longer to copy them to a local x and y if you want to.


-- 
Adam D. Ruppe
http://arsdnet.net



More information about the Digitalmars-d mailing list