assignment: left-to-right or right-to-left evaluation?
Nick Sabalausky
a at a.a
Mon May 11 13:30:59 PDT 2009
"Michiel Helvensteijn" <m.helvensteijn.remove at gmail.com> wrote in message
news:gua0ub$130b$1 at digitalmars.com...
> Nick Sabalausky wrote:
>> I was giving a little bit of thought to assignment chaining the other
>> day.
>> Unless someone can point out why I'm wrong, I think some of the
>> functional-style stuff we've been getting into can make assignment
>> chaining obsolete.
>>
>> Hypothetical example:
>> [mydic[x], mydic[y], mydic[z]].fill(mydic.length);
>>
>> I think something like that would be more clear than both the "tmp" and
>> assignment chaining versions, and perhaps allow any language complexities
>> that arise from the assignment chaining feature to be removed.
>
> Seriously?
>
> [mydic[x], mydic[y], mydic[z]].fill(mydic.length);,
>
> I admit, is not bad, but it's certainly not easier on the eyes than
>
> mydic[x] = mydic[y] = mydic[z] = mydic.length;.
>
True, that's why I replied again and suggested something like:
[mydic[x], mydic[y], mydic[z]].each = mydic.length;
Although that might be more difficult to implement properly.
> And you need a good optimizing compiler to get the same speed.
True, although as our compilers progress that will hopefully become less and
less of an issue. And in the meantime, if you really needed the speed, you
could make a templated string mixin that does the "tmp" version:
// Assuming we actually need the extra speed in this case:
mixin(fill!([mydic[x], mydic[y], mydic[z]], mydic.length));
// (or something roughly like that)
> Plus, doesn't
> it suffer from the same problems as the assignment-chaining version? If x,
> y and/or z are not already in mydic, which mydic.length are you using
> there?
>
At least in the case of the ".fill()" version, the function-call syntax
makes it clear that you're using the original "mydic.length". Other than
that, I'm convinced that order-of-function-call for assignments should be
rhs first, then lhs, regardless of what syntax is used to assign an rvalue
to multiple lvalues.
More information about the Digitalmars-d
mailing list