Tuple mixins
Daniel Keep
daniel.keep.lists at gmail.com
Fri May 11 04:52:17 PDT 2007
Max Samukha wrote:
> [...]
>
> Using your rule of thumb I discovered that expressions separated by
> comma, in parentheses, can be used in contexts where tuples are
> allowed. Seems like D is going to have built-in expression tuple
> literals. Cool. Has it been discussed anywhere?
>
> void foo(int a /*, int b, int c*/)
> {
> }
>
> void main()
> {
> auto tuple = (1, 2, 3);
> auto arr = [tuple]; // arr = [(1, 2, 3)] compiles too
> foo(tuple);
> }
I think you're confused as to what's happening. From the D spec:
"""
Expressions
Expression:
AssignExpression
AssignExpression , Expression
The left operand of the , is evaluated, then the right operand is
evaluated. The type of the expression is the type of the right operand,
and the result is the result of the right operand.
"""
What's happening is that (1, 2, 3) evaluates to *3*. If you did this:
(writefln("foo"), writefln("bar"), 42), it would print out "foo" and
"bar" and evaluate to 42.
They're not tuples, otherwise your foo(tuple) call would have failed,
since you would have had three values for a function that only takes
one. If you print out "arr", I think you'll find it's equal to [3] :P
This is one thing I've never especially liked about C, C++ and now D. I
personally thing that the comma should be used to construct tuples like
it is in Python, which is a hell of a lot more useful. Plus, this
behaviour is really friggin' weird :P
-- Daniel
--
int getRandomNumber()
{
return 4; // chosen by fair dice roll.
// guaranteed to be random.
}
http://xkcd.com/
v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
More information about the Digitalmars-d-learn
mailing list