3

As a matter of fact, solidity seems to have same behaviour of C in nested assignment.

I.e. running the sample test contract below, calling nestedSum(1) results in the following: a=38, b=16, c=4

pragma solidity 0.5.1;

contract test { 
    uint256 public a;
    uint256 public b;
    uint256 public c;

    function nestedSum (uint256 d) public
    {
        a = (b = (c = d + 3) + 12) + 22;
    }
}

Is this behavior described in some part of the documentation and is it guaranted to remain stable in the versioning of compilers?

Many thanks.

  • 1
    The answer to your question is No (in fact, even the return value of the assignment operator isn't described anywhere). – goodvibration Dec 11 '18 at 10:54
  • Thank you. I appreciate your point. I have the doubt that given an order of evaluation to operators, being assignment operator surely one of them, this behavior can be considered implicitly described. – Rick Park Dec 11 '18 at 11:12
  • Sorry, I don't understand your doubt (the logical structure of everything that comes after "doubt"). – goodvibration Dec 11 '18 at 11:36
  • I mean: is this behavior considered implicit by the compiler designer given that he stated that assignment operator has the lower order of evaluation (except for comma operator)? This means that it is assured that after the operations execution, the last value on the stack be the valued assigned. At this point if that is accessible (and at the moment seems it is accessible by far!) the behavior is assured. – Rick Park Dec 11 '18 at 11:46
  • 1
    Well, the documentation doesn't even specify that this operator yields a returned value (though I suppose that it would be very hard to generate a compiled code which doesn't leave this value in the stack, which is essentially equivalent to returning a value). Regardless of that, I think it's bad practice to write code this way (i.e., anything other than a = b = c, in opposed to your example). – goodvibration Dec 11 '18 at 12:13
0

Given that no answer are present, but some comments and some offline research have been done, I answer to my previous question for the sake of clarity.

In short the answer is that this is not mentioned in the official documentation but given that it:

1) is easy to obtain as consequence of the natural behavior of stack based computation 2) is accepted without hassle by the compiler 3) permit to avoid temporary variables in many cases

it is nothing to be considered stranger than a ‘++k;’ or ‘a /= 2;’ because this kind of short form derive exactly from the stack nature of the machine.

But it is not formalized as a separate element of the language.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.