Skip to content

Commit ef8911a

Browse files
authored
Merge pull request #11999 from nikitastupin/develop
[docs] Separate "Visibility and getters" section for state variables and functions
2 parents f386ed2 + 0f7b694 commit ef8911a

File tree

1 file changed

+38
-25
lines changed

1 file changed

+38
-25
lines changed

docs/contracts/visibility-and-getters.rst

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,41 @@
11
.. index:: ! visibility, external, public, private, internal
22

3+
.. |visibility-caveat| replace:: Making something ``private`` or ``internal`` only prevents other contracts from reading or modifying the information, but it will still be visible to the whole world outside of the blockchain.
4+
35
.. _visibility-and-getters:
46

57
**********************
68
Visibility and Getters
79
**********************
810

9-
Solidity knows two kinds of function calls: internal
10-
ones that do not create an actual EVM call (also called
11-
a "message call") and external
12-
ones that do. Because of that, there are four types of visibility for
13-
functions and state variables.
11+
State Variable Visibility
12+
=========================
13+
14+
``public``
15+
Public state variables differ from internal ones only in that the compiler automatically generates
16+
:ref:`getter functions<getter-functions>` for them, which allows other contracts to read their values.
17+
When used within the same contract, the external access (e.g. ``this.x``) invokes the getter
18+
while internal access (e.g. ``x``) gets the variable value directly from storage.
19+
Setter functions are not generated so other contracts cannot directly modify their values.
20+
21+
``internal``
22+
Internal state variables can only be accessed from within the contract they are defined in
23+
and in derived contracts.
24+
They cannot be accessed externally.
25+
This is the default visibility level for state variables.
26+
27+
``private``
28+
Private state variables are like internal ones but they are not visible in derived contracts.
29+
30+
.. warning::
31+
|visibility-caveat|
1432

15-
Functions have to be specified as being ``external``,
16-
``public``, ``internal`` or ``private``.
17-
For state variables, ``external`` is not possible.
33+
Function Visibility
34+
===================
35+
36+
Solidity knows two kinds of function calls: external ones that do create an actual EVM message call and internal ones that do not.
37+
Furthermore, internal functions can be made inaccessible to derived contracts.
38+
This gives rise to four types of visibility for functions.
1839

1940
``external``
2041
External functions are part of the contract interface,
@@ -24,27 +45,19 @@ For state variables, ``external`` is not possible.
2445

2546
``public``
2647
Public functions are part of the contract interface
27-
and can be either called internally or via
28-
messages. For public state variables, an automatic getter
29-
function (see below) is generated.
48+
and can be either called internally or via message calls.
3049

3150
``internal``
32-
Those functions and state variables can only be
33-
accessed internally (i.e. from within the current contract
34-
or contracts deriving from it), without using ``this``.
35-
This is the default visibility level for state variables.
51+
Internal functions can only be accessed from within the current contract
52+
or contracts deriving from it.
53+
They cannot be accessed externally.
54+
Since they are not exposed to the outside through the contract's ABI, they can take parameters of internal types like mappings or storage references.
3655

3756
``private``
38-
Private functions and state variables are only
39-
visible for the contract they are defined in and not in
40-
derived contracts.
41-
42-
.. note::
43-
Everything that is inside a contract is visible to
44-
all observers external to the blockchain. Making something ``private``
45-
only prevents other contracts from reading or modifying
46-
the information, but it will still be visible to the
47-
whole world outside of the blockchain.
57+
Private functions are like internal ones but they are not visible in derived contracts.
58+
59+
.. warning::
60+
|visibility-caveat|
4861

4962
The visibility specifier is given after the type for
5063
state variables and between parameter list and

0 commit comments

Comments
 (0)