You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
.. |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
+
3
5
.. _visibility-and-getters:
4
6
5
7
**********************
6
8
Visibility and Getters
7
9
**********************
8
10
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|
14
32
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.
18
39
19
40
``external``
20
41
External functions are part of the contract interface,
@@ -24,27 +45,19 @@ For state variables, ``external`` is not possible.
24
45
25
46
``public``
26
47
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.
30
49
31
50
``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.
36
55
37
56
``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|
48
61
49
62
The visibility specifier is given after the type for
0 commit comments