D and expression evaluation order.

Bruno Medeiros brunodomedeiros+spam at com.gmail
Fri Apr 27 04:38:00 PDT 2007


Manfred Nowak wrote:
> Walter Bright wrote
> 
>> Then I'd have a pile of bug reports because it isn't so easy to
>> implement.
> 
> Why that, when it isn't in the specs, only in this forum, and declared 
> as a draft definition?
> 
> Please do not declare that the thinking capabilities of average D-users 
> are that low.
> 
> -manfred

Someone with a non-low average thinking capability (*grin* :) ) would 
easily deduce that the evaluation order will be the same as any other 
modern programming language (not just Java, but C#, Python, even Ruby, 
have all the same evaluation order). The rule in a general sense is:
   Evaluate operands in the same order as the associativity rule of the 
operator.

So stuff like this:
   c = a + (b = c);
evaluates in this order:
   a
   c
   b
   (b = c)
   a + (b = c)
   c
   c = a + (b = c)

A function call, since it is basicly an operator that takes a function 
value and arguments as operands, works in the same rule. So:
   func(++i, ++c);
evaluates:
   func
   ++i
   ++c
   func(++i, ++c)

I don't think there is any significant learning process involved here. 
If you know the associativity rules (which you should), then the 
evaluation order is simply what naturally follows from there. You don't 
have to memorize a whole different set of rules. In fact the assignments 
are basicly the only operators where the operands are evaluated 
right-to-left.

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



More information about the Digitalmars-d mailing list