Multiple assignment

bearophile bearophileHUGS at lycos.com
Fri Feb 25 17:09:34 PST 2011


simendsjo:

> 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
> """

Right. But my code was wrong, I meant to write this:

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, 1]); // Not OK
}

And I think it is working as you expect it to.

Thank you and sorry for the noise,
bearophile


More information about the Digitalmars-d-learn mailing list