Assignment Expression Idea (tuples)

Wolfgang Draxinger wdraxinger at darkstargames.de
Fri Jun 9 13:02:45 PDT 2006


Ben Hinkle wrote:
 
> Changing the , operator to make tuples is a pretty big change. In
> particular it would have to change precedence.

I understand what you mean and

i = a, j = b;

would be ambigous if the expression evaluated left to right.

> This is backwards from how MATLAB (and Cx) does it. The assignment goes
> left to right. If you return more values that destinations then unused
> values are discarded (much like you can discard the return value of a
> regular one-output function).

LTR is also how Lua and Python do it, but the RTL reading would have
retained the old semantics, and it would solve the ambiguty of above
example. In the current syntax, which is identical to C
<http://www.digitalmars.com/d/expression.html#Expression>

i = a, j = b

i would get assigned the value of the last statement in the comma operator,
which is j, which gets assigned b. With the RTL-tuple syntax this would
mean "assign the last element of the tupel (a, j) to i, which is j, and j
gets assigned by b. a wouldn't be affected at all".

This is, why I used RTL syntax: It keeps the old semantics and doesn't
introduce ambiguties and keeps the operator precedence.

OTOH LTR reading is much more common for tuple expressions. But I think
tuples should be introduced into D as a syntactic sugar. For example one
could use tuples to assign values to arrays (static and dynamic) and
intialize static arrays with them. This would naturally introduce []
brackets as a tuple designator if used as a L value. This brings me to
another idea: Using tuples as index values, i.e. if a tuple is used as an
index for array dereferencing the result is a tuple of the indexed array
elements (which could the again be used as array initializer).

int[] A;
int[5] B=A[[1, 5, 10, 3, 4]];

Or
A[[2, 7, 6, 8]] = [1, 2, 3, 4];

And another implication
[1, 2, 3, 4][0] == 1;
[1, 2, 3, 4][1] == 2;
.
.
.

The special case would be, if a tuple is used as an L value. In this case
the contents of the tuple must be L values, too, and the tuple implicitly
holds references.

Wolfgang Draxinger




More information about the Digitalmars-d mailing list