www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - [D1][expressions] Order Of Evaluation

reply %u <e ee.com> writes:
/The following binary expressions are evaluated in an implementation-defined
order:
AssignExpression/../AddExpression/

/It is an error to depend on order of evaluation when it is not specified./

That makes this an error!?

y = x + 1;

Am I being paranoid or should I be adding more brackets?
Oct 08 2010
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
%u:

 That makes this an error!?
 
 y = x + 1;
 
 Am I being paranoid or should I be adding more brackets?

I presume this doesn't need other brackets. And Walter has two or three times stated that he wants to eventually define the order of evaluation in D (as C#/Java), I hope this will happen. Bye, bearophile
Oct 08 2010
prev sibling parent reply "Denis Koroskin" <2korden gmail.com> writes:
On Fri, 08 Oct 2010 18:49:36 +0400, %u <e ee.com> wrote:

 /The following binary expressions are evaluated in an  
 implementation-defined
 order:
 AssignExpression/../AddExpression/

 /It is an error to depend on order of evaluation when it is not  
 specified./

 That makes this an error!?

 y = x + 1;

 Am I being paranoid or should I be adding more brackets?

Assignment has higher precedence that addition so your code has no errors. However, the following one does: int x = 1; int y = (x = 2) + x; because "x" and "x = 2" has same precedence and thus may be evaluated in any order. Stay away from such code and you should be fine.
Oct 08 2010
parent %u <e ee.com> writes:
== Quote from Denis Koroskin (2korden gmail.com)'s article
 On Fri, 08 Oct 2010 18:49:36 +0400, %u <e ee.com> wrote:
 /The following binary expressions are evaluated in an
 implementation-defined
 order:
 AssignExpression/../AddExpression/

 /It is an error to depend on order of evaluation when it is not
 specified./

 That makes this an error!?

 y = x + 1;

 Am I being paranoid or should I be adding more brackets?


But you are right, I am confusing precedence and associativity. The expressions page reads like there are only two levels of precedence, but it only says anyting about the associativity. A quick search on precedence shows that the C precedence rules apply.
 However, the following one does:
 int x = 1;
 int y = (x = 2) + x;
 because "x" and "x = 2" has same precedence and thus may be evaluated in
 any order.
 Stay away from such code and you should be fine.

I never assign within assignments :)
Oct 08 2010