D and expression evaluation order.

Bruno Medeiros brunodomedeiros+spam at com.gmail
Thu Apr 26 03:47:47 PDT 2007


As we know, in C/C++ there are a lot of cases where the order of
evaluation of an expression is undefined.

   i = i++;
   c = a + (a = b);
   func(++i, ++i);

D goes one step further by defining that any such behavior is illegal:
"Unless otherwise specified, the implementation is free to evaluate the
components of an expression in any order. It is an error to depend on
order of evaluation when it is not specified." in
http://www.digitalmars.com/d/expression.html .

That's nice, but why not go all the way, and actually define an
evaluation order for such expressions. There is nothing to lose, and it 
should be easy to implement. This is what Java does. For
example, the following (which I found recently in JDT's code) is
perfectly legal Java code:

   int length = array.length;
   ...
   System.arraycopy(array, 0, array = new IFoo[length + 1], 0, length);

because Java not only defines that the argument evaluation order is left
to right, but also that the arguments are bound to parameters as they
are evaluated (and not after all are evaluated).

The little details matter a lot.

-- 
Bruno Medeiros - MSc in CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d mailing list