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

Tom S h3r3tic at remove.mat.uni.torun.pl
Thu Jul 23 14:49:49 PDT 2009


Walter Bright wrote:
> Of course that's true, but why is a dedicated syntax better than Tuple!( 
> ...) ?

Why is dedicated syntax for comma expressions better than commaExpr(...) 
? I take it most folks on this NG would prefer to have shorter syntax 
for tuples instead the virtually never used comma expressions.

Other than that, you need Tuple!(...) for type tuples, some sorta 
STuple!(...) for struct-tuples (so they can be nested and passed around 
properly) and makeTuple(...) for struct-tuple instances.

Comma expressions don't even have to be first-class members of the language:

----

import tango.io.Stdout;

T[$-1] commaExpr(T ...)(lazy T t) { foreach (x; t[0..$-1]) { auto y = x; 
} return t[$-1]; }
T[0] revCommaExpr(T ...)(lazy T t) { foreach_reverse (x; t[1..$]) { auto 
y = x; } return t[0]; }

void main() {
     {
         int x;
         int y = revCommaExpr(x += 123, x *= 2, Stdout.formatln("{}", 
x), Stdout("omg").newline);
		assert (y == 123);
     }

     {
         int x;
         commaExpr(x += 123, x *= 2, Stdout.formatln("{}", x), 
Stdout("omg")).newline;
     }
}

----
Output:

omg
0
246
omg
----



-- 
Tomasz Stachowiak
http://h3.team0xf.com/
h3/h3r3tic on #D freenode



More information about the Digitalmars-d mailing list