assignment: left-to-right or right-to-left evaluation?
Frank Benoit
keinfarbton at googlemail.com
Sat May 9 10:06:27 PDT 2009
Andrei Alexandrescu schrieb:
> Consider:
>
> uint fun();
> int gun();
> ....
> int[] a = new int[5];
> a[fun] = gun;
>
> Which should be evaluated first, fun() or gun()? It's a rather arbitrary
> decision. C/C++ don't even define an order. Python chooses
> left-to-right, EXCEPT for assignment, which is right-hand side first.
> Lisp and C# choose consistent left-to-right. I don't like exceptions and
> I'd like everything to be left-to-right. However, this leads to some odd
> cases. Consider this example in TDPL:
>
> import std.stdio, std.string;
>
> void main() {
> uint[string] dic;
> foreach (line; stdin.byLine) {
> string[] words = split(strip(line));
> foreach (word; words) {
> if (word in dic) continue; // nothing to do
> uint newID = dic.length;
> dic[word] = newID;
> writeln(newID, '\t', word);
> }
> }
> }
>
> If we want to get rid of newID, we'd write:
>
> writeln(dic.length, '\t', word);
> dic[word] = dic.length;
>
> by the Python rule, and
>
> writeln(dic.length, '\t', word);
> dic[word] = dic.length - 1;
>
> by the C# rule.
>
> What's best?
>
>
> Andrei
>From my POV, it would be nice if it would be the same as in Java,
because i am porting lots of it to D.
More information about the Digitalmars-d
mailing list