Solidity

Solidity Operators Involving LValues

Solidity Operators Involving LValues Main Tips

  • An LValue is a variable or something, which can have something assigned to it.
  • There are operators that can be used as shorthands for LValues.

Solidity Operators Involving LValues

If is an LValue, the operators shown below are available as shorthands:

x += y is the same as x = x + y.

The operators -=*=/=%=x |=&= and ^= are all defined accordingly.

x++ and x– are the same as to x += 1 / x -= 1, however, the expression itself still possesses the previous value of x In contrast, –x and ++x have the same effect on x but return the value after the change.


Solidity Operators Involving LValues Delete

delete x will assign the initial value for the type to x. For example, in the case of integers, it is the same as to x = 0, however, it can also be utilized on arrays, where it would assign a dynamic array of length zero, or a static array that is the same length with all elements reset. As for structs, it will assign a struct along with every member reset.

delete will have no effect on whole mappings (since the mappings’ keys may be arbitrary and are overall unknown). So if a struct is deleted, it resets all members which are not mappings, additionally recursing into the members unless they are mappings. Despite this, individual keys along with what they map are possible to delete.

Additionally, delete a really acts like an assignment to x, for instance, it would store a new object in x.

Example

pragma solidity ^0.4.0;

contract exampleDelete {
    uint arrayData;
    uint[] exampleArray;

    function f() {
        uint x = arrayData;
        delete x; // sets x to 0, has no effect on arrayData
        delete arrayData; // this will set arrayData to 0, but not affect x which will still hold a copy
        uint[] y = exampleArray;
        delete exampleArray; // this would set dataArray.length to zero, however, as uint[] is a complex object,
        // y is affected too, it being an alias to the storage object
        // Despite this, "delete y" is not a valid statement, since assignments to local variables
        // that reference storage objects may only be made from pre-existing storage objects.
    }
}

 

Try on Remix Try live on Hosting

Read previous post:
Solidity Mappings

Solidity Mappings Main Tips Mapping can be interpreted as sort of hash tables that are initialized virtually so that each potential key...

Close