Tuple mixins
Max Samukha
samukha at voliacable.com.removethis
Fri May 11 05:15:04 PDT 2007
On Fri, 11 May 2007 21:52:17 +1000, Daniel Keep
<daniel.keep.lists at gmail.com> wrote:
>
>
>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.
>"""
I missed that one, thanks. Is there any use cases of the comma
separated expressions?
>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
>
I printed it out but thought it was a compiler bug.
>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
Absolutely agree.
More information about the Digitalmars-d-learn
mailing list