Multiple assignment
simendsjo
simen.endsjo at pandavre.com
Fri Feb 25 17:03:30 PST 2011
On 26.02.2011 01:56, bearophile wrote:
> Is this program showing a bug in multiple assignments (DMD 2.052)?
>
>
> void main() {
> int i;
> int[2] x;
> i, x[i] = 1;
> assert(x == [1, 0]); // OK
>
> int j;
> int[2] y;
> y[j], j = 1;
> assert(y == [0, 0]); // Not OK
> }
>
>
> At the end of the program I expect y to be [1,0] instead of [0,0].
>
> Yet this C program with GCC:
>
> #include "stdio.h"
> int main() {
> int i = 0;
> int x[2] = {0, 0};
> i, x[i] = 1;
> printf("%d %d\n", x[0], x[1]);
>
> int j = 0;
> int y[2] = {0, 0};
> y[j], j = 1;
> printf("%d %d\n", y[0], y[1]);
>
> return 0;
> }
>
>
> has the same output as DMD:
> 1 0
> 0 0
>
> Bye,
> bearophile
I couldn't find any info on the comma expression in the language
reference, but this was my first google hit:
"""
A comma expression contains two operands of any type separated by a
comma and has *left-to-right* associativity. The left operand is fully
evaluated, possibly producing side effects, and its value, if there is
one, is *discarded*. The right operand is then evaluated. The type and
value of the result of a comma expression are those of its right
operand, after the usual unary conversions
"""
More information about the Digitalmars-d-learn
mailing list