Update reference types.

This commit is contained in:
chriseth 2019-12-17 14:24:18 +01:00
parent d13438eed8
commit 11b90301b0

View File

@ -214,25 +214,34 @@ Array Members
**length**: **length**:
Arrays have a ``length`` member that contains their number of elements. Arrays have a ``length`` member that contains their number of elements.
The length of memory arrays is fixed (but dynamic, i.e. it can depend on runtime parameters) once they are created. The length of memory arrays is fixed (but dynamic, i.e. it can depend on
**push**: runtime parameters) once they are created.
Dynamic storage arrays and ``bytes`` (not ``string``) have a member function called ``push`` that you can use to append an element at the end of the array. **push()**:
If no argument is given, the element will be zero-initialised and a reference to the new element is returned. Dynamic storage arrays and ``bytes`` (not ``string``) have a member function
If a value is given as argument, ``push`` returns nothing. called ``push()`` that you can use to append a zero-initialised element at the end of the array.
It returns a reference to the element, so that it can be used like
``x.push().t = 2`` or ``x.push() = b``.
**push(x)**:
Dynamic storage arrays and ``bytes`` (not ``string``) have a member function
called ``push(x)`` that you can use to append a given element at the end of the array.
The function returns nothing.
**pop**: **pop**:
Dynamic storage arrays and ``bytes`` (not ``string``) have a member function called ``pop`` that you can use to remove an element from the end of the array. This also implicitly calls :ref:`delete<delete>` on the removed element. Dynamic storage arrays and ``bytes`` (not ``string``) have a member
function called ``pop`` that you can use to remove an element from the
end of the array. This also implicitly calls :ref:`delete<delete>` on the removed element.
.. note:: .. note::
Increasing the length of a storage array by calling ``push()`` Increasing the length of a storage array by calling ``push()``
has constant gas costs because storage is zero-initialised, has constant gas costs because storage is zero-initialised,
while decreasing the length by calling ``pop()`` has at least while decreasing the length by calling ``pop()`` has a
linear cost (but in most cases worse than linear), cost that depends on the "size" of the element being removed.
because it includes explicitly clearing the removed If that element is an array, it can be very costly, because
it includes explicitly clearing the removed
elements similar to calling :ref:`delete<delete>` on them. elements similar to calling :ref:`delete<delete>` on them.
.. note:: .. note::
It is not yet possible to use arrays of arrays in external functions To use arrays of arrays in external (instead of public) functions, you need to
(but they are supported in public functions). activate ABIEncoderV2.
.. note:: .. note::
In EVM versions before Byzantium, it was not possible to access In EVM versions before Byzantium, it was not possible to access
@ -285,7 +294,8 @@ Array Members
} }
function changeFlagArraySize(uint newSize) public { function changeFlagArraySize(uint newSize) public {
// if the new size is smaller, removed array elements will be cleared // using push and pop is the only way to change the
// length of an array
if (newSize < m_pairsOfFlags.length) { if (newSize < m_pairsOfFlags.length) {
while (m_pairsOfFlags.length > newSize) while (m_pairsOfFlags.length > newSize)
m_pairsOfFlags.pop(); m_pairsOfFlags.pop();
@ -412,13 +422,18 @@ shown in the following example:
pragma solidity >=0.4.11 <0.7.0; pragma solidity >=0.4.11 <0.7.0;
contract CrowdFunding { // Defines a new type with two fields.
// Defines a new type with two fields. // Declaring a struct outside of a contract allows
struct Funder { // it to be shared by multiple contracts.
address addr; // Here, this is not really needed.
uint amount; struct Funder {
} address addr;
uint amount;
}
contract CrowdFunding {
// Structs can also be defined inside contracts, which makes them
// visible only there and in derived contracts.
struct Campaign { struct Campaign {
address payable beneficiary; address payable beneficiary;
uint fundingGoal; uint fundingGoal;