Assignment Expression Idea

Wolfgang Draxinger wdraxinger at darkstargames.de
Thu Jun 8 07:20:32 PDT 2006


Lastly I was writing a huge buck of Python code and then came
back to D and unintentionally used a nice syntax sugar (getting
a compile error), that's possible in a lot of cool languages,
among them Python and Lua (maybe one of the next D versions?).

Say you got a bunch of variables:

int a1, a2;
char b1, b2;
float d1, d2;
class T{...}; T t1, t2;

Then in Python you can perfectly write

a1, b1, c1, t1 = a2, b2, c2, t2;

but also such fancy stuff like swapping

a1, a2 = a2, a1;

and if the types are compatible or there have been conversion
operators been introduced

d1, t1 = a1, b1;
d2, t2 = a2, a2; // a2 get's assigned to both d2 and t2

I don't think that this would introduce ambiguties. The semantics
of the ','-operator could be changed, that the result is a tuple
and that tuples get read and assigned right to left (making
tuples a L value). If for example one writes 

a1, a2, d1 = b1, b2;

This would evaluate

d1 = b2;
a2 = b1;
a1 = nil;

Well, nil is not defined this way in D and thus I'd let raise
this a syntax error, but

a1, a2 = b1, b2, b3;

would be safe

a2 = b3;
a1 = b2;

No assignment of b1, which is consistent with the old syntax.
An implementation must take care, that in assignments like

a1, a2 = a2, a1;

temporary registers are used.

-- 
Wolfgang Draxinger




More information about the Digitalmars-d mailing list