Tuple assignment
Tomek Sowiński
just at ask.me
Thu Oct 7 12:02:50 PDT 2010
Walter Bright napisał:
> If expr represents a tuple, we (Andrei and I) were thinking about the
> syntax:
>
> auto (a, b, c, d) = expr;
>
> being equivalent to:
>
> auto t = expr; auto a = t[0]; auto b = t[1]; auto c = t[2 .. $];
Typo? If not, what is 'd'?
Either way, I'd like mismatching tuple lengths to fail, not assign the tail to the last variable.
Or, as pelle brought up: auto (a, b..., c) = expr, where b = expr[1..2] and you may have
only one ... in the lhs. It's not bad.
> You can also do this with arrays, such that:
>
> float[3] xyz;
> auto (x, y, z) = xyz;
>
> The Lithpers among you will notice that this essentially provides a handy
> car,cdr shortcut for tuples and arrays:
>
> auto (car, cdr) = expr;
Nice. It's all nice but as my colleague once said: put it on the todo list right after 'learn
Portugese'.
--
Tomek
More information about the Digitalmars-d
mailing list