To my understanding,
delete x merely sets the value of
x to the default value defined for the type of
x (which is typically 0 in one way or another).
uint x = 1; delete x; // sets x = 0 bool x = true; delete x; // sets x = false
The doc actually says it pretty explicitly:
It is important to note that
delete areally behaves like an assignment to
a, i.e. it stores a new object in
But my question is really with regards to mappings.
Suppose I have
mapping (address => bool) validAddresses and some
Are the following two statements equivalent:
validAddresses[x] = false;
Or is there any benefit in #2 over #1 (as there would be in "traditional" languages)?
The doc states something which might answer my question:
deletehas no effect on whole mappings (as the keys of mappings may be arbitrary and are generally unknown).
But I'm honestly having problems understanding it.
I do understand that
delete has no effect on anything other than mappings, in the sense that no memory is being freed as a result of executing it.
But I do not understand whether or not the same thing applies for mappings.
I went ahead and disassembled each one of these two statements via
/* "contracts/MyContract.sol":1490:1538 validAddresses[x] = false */ dup1 sload 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00 and swap1 sstore
/* "contracts/MyContract.sol":1490:1537 delete validAddresses[x] */ dup1 sload 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00 and swap1 sstore
The compiled code indicates that the two statements are identical, so I'm basically just looking for a confirmation on this fact (in opposed to the answer below, which suggests that gas-refund applies only when
delete is used explicitly).