mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #1641 from ethereum/docs-getters
Rename accessor to getter
This commit is contained in:
commit
5ce79609a4
@ -145,11 +145,11 @@ This means that cyclic creation dependencies are impossible.
|
|||||||
|
|
||||||
.. index:: ! visibility, external, public, private, internal
|
.. index:: ! visibility, external, public, private, internal
|
||||||
|
|
||||||
.. _visibility-and-accessors:
|
.. _visibility-and-getters:
|
||||||
|
|
||||||
************************
|
**********************
|
||||||
Visibility and Accessors
|
Visibility and Getters
|
||||||
************************
|
**********************
|
||||||
|
|
||||||
Since Solidity knows two kinds of function calls (internal
|
Since Solidity knows two kinds of function calls (internal
|
||||||
ones that do not create an actual EVM call (also called
|
ones that do not create an actual EVM call (also called
|
||||||
@ -173,7 +173,7 @@ and the default is ``internal``.
|
|||||||
``public``:
|
``public``:
|
||||||
Public functions are part of the contract
|
Public functions are part of the contract
|
||||||
interface and can be either called internally or via
|
interface and can be either called internally or via
|
||||||
messages. For public state variables, an automatic accessor
|
messages. For public state variables, an automatic getter
|
||||||
function (see below) is generated.
|
function (see below) is generated.
|
||||||
|
|
||||||
``internal``:
|
``internal``:
|
||||||
@ -243,12 +243,12 @@ In the following example, ``D``, can call ``c.getData()`` to retrieve the value
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.. index:: ! accessor;function, ! function;accessor
|
.. index:: ! getter;function, ! function;getter
|
||||||
|
|
||||||
Accessor Functions
|
Getter Functions
|
||||||
==================
|
================
|
||||||
|
|
||||||
The compiler automatically creates accessor functions for
|
The compiler automatically creates getter functions for
|
||||||
all **public** state variables. For the contract given below, the compiler will
|
all **public** state variables. For the contract given below, the compiler will
|
||||||
generate a function called ``data`` that does not take any
|
generate a function called ``data`` that does not take any
|
||||||
arguments and returns a ``uint``, the value of the state
|
arguments and returns a ``uint``, the value of the state
|
||||||
@ -271,7 +271,7 @@ be done at declaration.
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
The accessor functions have external visibility. If the
|
The getter functions have external visibility. If the
|
||||||
symbol is accessed internally (i.e. without ``this.``),
|
symbol is accessed internally (i.e. without ``this.``),
|
||||||
it is evaluated as a state variable and if it is accessed externally
|
it is evaluated as a state variable and if it is accessed externally
|
||||||
(i.e. with ``this.``), it is evaluated as a function.
|
(i.e. with ``this.``), it is evaluated as a function.
|
||||||
@ -462,7 +462,7 @@ Functions can be declared constant. These functions promise not to modify the st
|
|||||||
}
|
}
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
Accessor methods are marked constant.
|
Getter methods are marked constant.
|
||||||
|
|
||||||
.. warning::
|
.. warning::
|
||||||
The compiler does not enforce yet that a constant method is not modifying state.
|
The compiler does not enforce yet that a constant method is not modifying state.
|
||||||
@ -882,7 +882,7 @@ Inheriting Different Kinds of Members of the Same Name
|
|||||||
|
|
||||||
When the inheritance results in a contract with a function and a modifier of the same name, it is considered as an error.
|
When the inheritance results in a contract with a function and a modifier of the same name, it is considered as an error.
|
||||||
This error is produced also by an event and a modifier of the same name, and a function and an event of the same name.
|
This error is produced also by an event and a modifier of the same name, and a function and an event of the same name.
|
||||||
As an exception, a state variable accessor can override a public function.
|
As an exception, a state variable getter can override a public function.
|
||||||
|
|
||||||
.. index:: ! contract;abstract, ! abstract contract
|
.. index:: ! contract;abstract, ! abstract contract
|
||||||
|
|
||||||
|
@ -393,7 +393,7 @@ Currently, Solidity automatically generates a runtime exception in the following
|
|||||||
#. If you convert a value too big or negative into an enum type.
|
#. If you convert a value too big or negative into an enum type.
|
||||||
#. If you perform an external function call targeting a contract that contains no code.
|
#. If you perform an external function call targeting a contract that contains no code.
|
||||||
#. If your contract receives Ether via a public function without ``payable`` modifier (including the constructor and the fallback function).
|
#. If your contract receives Ether via a public function without ``payable`` modifier (including the constructor and the fallback function).
|
||||||
#. If your contract receives Ether via a public accessor function.
|
#. If your contract receives Ether via a public getter function.
|
||||||
#. If you call a zero-initialized variable of internal function type.
|
#. If you call a zero-initialized variable of internal function type.
|
||||||
|
|
||||||
Internally, Solidity performs an "invalid jump" when a user-provided exception is thrown. In contrast, it performs an invalid operation
|
Internally, Solidity performs an "invalid jump" when a user-provided exception is thrown. In contrast, it performs an invalid operation
|
||||||
|
@ -641,7 +641,7 @@ Not yet, as this requires two levels of dynamic arrays (``string`` is a dynamic
|
|||||||
If you issue a call for an array, it is possible to retrieve the whole array? Or must you write a helper function for that?
|
If you issue a call for an array, it is possible to retrieve the whole array? Or must you write a helper function for that?
|
||||||
===========================================================================================================================
|
===========================================================================================================================
|
||||||
|
|
||||||
The automatic accessor function for a public state variable of array type only returns
|
The automatic getter function for a public state variable of array type only returns
|
||||||
individual elements. If you want to return the complete array, you have to
|
individual elements. If you want to return the complete array, you have to
|
||||||
manually write a function to do that.
|
manually write a function to do that.
|
||||||
|
|
||||||
|
@ -131,7 +131,7 @@ too far, though, as it is neither possible to obtain a list of all keys of
|
|||||||
a mapping, nor a list of all values. So either keep in mind (or
|
a mapping, nor a list of all values. So either keep in mind (or
|
||||||
better, keep a list or use a more advanced data type) what you
|
better, keep a list or use a more advanced data type) what you
|
||||||
added to the mapping or use it in a context where this is not needed,
|
added to the mapping or use it in a context where this is not needed,
|
||||||
like this one. The accessor function created by the ``public`` keyword
|
like this one. The getter function created by the ``public`` keyword
|
||||||
is a bit more complex in this case. It roughly looks like the
|
is a bit more complex in this case. It roughly looks like the
|
||||||
following::
|
following::
|
||||||
|
|
||||||
|
@ -427,7 +427,7 @@ Tips and Tricks
|
|||||||
|
|
||||||
* Use ``delete`` on arrays to delete all its elements.
|
* Use ``delete`` on arrays to delete all its elements.
|
||||||
* Use shorter types for struct elements and sort them such that short types are grouped together. This can lower the gas costs as multiple SSTORE operations might be combined into a single (SSTORE costs 5000 or 20000 gas, so this is what you want to optimise). Use the gas price estimator (with optimiser enabled) to check!
|
* Use shorter types for struct elements and sort them such that short types are grouped together. This can lower the gas costs as multiple SSTORE operations might be combined into a single (SSTORE costs 5000 or 20000 gas, so this is what you want to optimise). Use the gas price estimator (with optimiser enabled) to check!
|
||||||
* Make your state variables public - the compiler will create :ref:`getters <visibility-and-accessors>` for you for free.
|
* Make your state variables public - the compiler will create :ref:`getters <visibility-and-getters>` for you for free.
|
||||||
* If you end up checking conditions on input or state a lot at the beginning of your functions, try using :ref:`modifiers`.
|
* If you end up checking conditions on input or state a lot at the beginning of your functions, try using :ref:`modifiers`.
|
||||||
* If your contract has a function called ``send`` but you want to use the built-in send-function, use ``address(contractVariable).send(amount)``.
|
* If your contract has a function called ``send`` but you want to use the built-in send-function, use ``address(contractVariable).send(amount)``.
|
||||||
* Initialise storage structs with a single assignment: ``x = MyStruct({a: 1, b: 2});``
|
* Initialise storage structs with a single assignment: ``x = MyStruct({a: 1, b: 2});``
|
||||||
@ -541,7 +541,7 @@ Function Visibility Specifiers
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
- ``public``: visible externally and internally (creates accessor function for storage/state variables)
|
- ``public``: visible externally and internally (creates getter function for storage/state variables)
|
||||||
- ``private``: only visible in the current contract
|
- ``private``: only visible in the current contract
|
||||||
- ``external``: only visible externally (only for functions) - i.e. can only be message-called (via ``this.func``)
|
- ``external``: only visible externally (only for functions) - i.e. can only be message-called (via ``this.func``)
|
||||||
- ``internal``: only visible internally
|
- ``internal``: only visible internally
|
||||||
|
@ -28,7 +28,7 @@ State variables are values which are permanently stored in contract storage.
|
|||||||
}
|
}
|
||||||
|
|
||||||
See the :ref:`types` section for valid state variable types and
|
See the :ref:`types` section for valid state variable types and
|
||||||
:ref:`visibility-and-accessors` for possible choices for
|
:ref:`visibility-and-getters` for possible choices for
|
||||||
visibility.
|
visibility.
|
||||||
|
|
||||||
.. _structure-functions:
|
.. _structure-functions:
|
||||||
@ -49,7 +49,7 @@ Functions are the executable units of code within a contract.
|
|||||||
}
|
}
|
||||||
|
|
||||||
:ref:`function-calls` can happen internally or externally
|
:ref:`function-calls` can happen internally or externally
|
||||||
and have different levels of visibility (:ref:`visibility-and-accessors`)
|
and have different levels of visibility (:ref:`visibility-and-getters`)
|
||||||
towards other contracts.
|
towards other contracts.
|
||||||
|
|
||||||
.. _structure-function-modifiers:
|
.. _structure-function-modifiers:
|
||||||
|
@ -543,8 +543,8 @@ So ``bytes`` should always be preferred over ``byte[]`` because it is cheaper.
|
|||||||
that you are accessing the low-level bytes of the UTF-8 representation,
|
that you are accessing the low-level bytes of the UTF-8 representation,
|
||||||
and not the individual characters!
|
and not the individual characters!
|
||||||
|
|
||||||
It is possible to mark arrays ``public`` and have Solidity create an accessor.
|
It is possible to mark arrays ``public`` and have Solidity create a getter.
|
||||||
The numeric index will become a required parameter for the accessor.
|
The numeric index will become a required parameter for the getter.
|
||||||
|
|
||||||
.. index:: ! array;allocating, new
|
.. index:: ! array;allocating, new
|
||||||
|
|
||||||
@ -792,11 +792,11 @@ Because of this, mappings do not have a length or a concept of a key or value be
|
|||||||
Mappings are only allowed for state variables (or as storage reference types
|
Mappings are only allowed for state variables (or as storage reference types
|
||||||
in internal functions).
|
in internal functions).
|
||||||
|
|
||||||
It is possible to mark mappings ``public`` and have Solidity create an accessor.
|
It is possible to mark mappings ``public`` and have Solidity create a getter.
|
||||||
The ``_KeyType`` will become a required parameter for the accessor and it will
|
The ``_KeyType`` will become a required parameter for the getter and it will
|
||||||
return ``_ValueType``.
|
return ``_ValueType``.
|
||||||
|
|
||||||
The ``_ValueType`` can be a mapping too. The accessor will have one parameter
|
The ``_ValueType`` can be a mapping too. The getter will have one parameter
|
||||||
for each ``_KeyType``, recursively.
|
for each ``_KeyType``, recursively.
|
||||||
|
|
||||||
::
|
::
|
||||||
|
Loading…
Reference in New Issue
Block a user