Solidity: Doc: Order of Evaluation of Expressions

Created on 25 Jul 2018  路  4Comments  路  Source: ethereum/solidity

Given http://solidity.readthedocs.io/en/latest/control-structures.html#order-of-evaluation-of-expressions and given this example:

pragma solidity ^0.4.24;
contract A {
  uint a;
  uint b;
  uint c;

  function init() public{
    a = 10;
    b = 10;
    c = 20;
  }
  function t() public view returns(uint res){
    res = a * b / c;
  }

  function u() public returns(uint){
      a += 1;
      return a;
  }
  function v() public returns(uint){
      a += 2;
      return a;
  }
  function w() public returns(uint){
      return u() * v();
  }
}

I understand that the result of

init();
w();

is unspecified, given that either u() or v() could be evaluated first (last I tried, v() was evaluated first).

Is this correct? Adding such an example to the documentation could help.

Similarly, is the result of:

init();
t();

also unspecified? More precisely, given an expression containing operators of equal precedence (* and /), is the evaluation order unspecified, or is it guaranteed to be from left to right?

documentation

All 4 comments

I think we can guarantee the order of evaluation of operators of equal precedence, but the order of side-effecty expressions is not guaranteed.

@chriseth: Can you please add the associativity of operators to the documentation here?
https://solidity.readthedocs.io/en/v0.4.25/miscellaneous.html (assuming that it is well-defined in the compiler which, in my opinion it ought to be.)

@Sword-Smith yes, we should!

I think this has been done.

Was this page helpful?
0 / 5 - 0 ratings