assignment: left-to-right or right-to-left evaluation?
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Sat May 9 09:43:09 PDT 2009
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
More information about the Digitalmars-d
mailing list