diff options
author | chriseth <c@ethdev.com> | 2016-07-09 02:15:28 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-09 02:15:28 +0800 |
commit | efad1e05ac97c9460846778af566a7ec7f37d207 (patch) | |
tree | 3534d80ee4667f454a4cec019b99a84690c367dc /docs | |
parent | e5061615cef6687fd241051945b4be929945a62b (diff) | |
parent | cf0579a86eede7cf9b57695f5fa6557c65d4f5a8 (diff) | |
download | dexon-solidity-efad1e05ac97c9460846778af566a7ec7f37d207.tar.gz dexon-solidity-efad1e05ac97c9460846778af566a7ec7f37d207.tar.zst dexon-solidity-efad1e05ac97c9460846778af566a7ec7f37d207.zip |
Merge pull request #680 from Denton-L/fix-679
Add section about default values of variables
Diffstat (limited to 'docs')
-rw-r--r-- | docs/control-structures.rst | 14 | ||||
-rw-r--r-- | docs/types.rst | 2 |
2 files changed, 12 insertions, 4 deletions
diff --git a/docs/control-structures.rst b/docs/control-structures.rst index ab6f59fb..064996ac 100644 --- a/docs/control-structures.rst +++ b/docs/control-structures.rst @@ -161,14 +161,20 @@ Complications for Arrays and Structs The semantics of assignment are a bit more complicated for non-value types like arrays and structs. Assigning *to* a state variable always creates an independent copy. On the other hand, assigning to a local variable creates an independent copy only for elementary types, i.e. static types that fit into 32 bytes. If structs or arrays (including ``bytes`` and ``string``) are assigned from a state variable to a local variable, the local variable holds a reference to the original state variable. A second assignment to the local variable does not modify the state but only changes the reference. Assignments to members (or elements) of the local variable *do* change the state. -.. index:: ! exception, ! throw +.. index:: ! scoping, declarations, default value + +.. _default-value: Scoping and Declarations ======================== -.. index:: ! scoping, ! declarations +A variable which is declared will have an initial default value whose byte-representation is all zeros. +The "default values" of variables are the typical "zero-state" of whatever the type is. For example, the default value for a ``bool`` +is ``false``. The default value for the ``uint`` or ``int`` types is ``0``. For statically-sized arrays and ``bytes1`` to ``bytes32``, each individual +element will be initialized to the default value corresponding to its type. Finally, for dynamically-sized arrays, ``bytes`` +and ``string``, the default value is an empty array or string. -In Solidity, a variable declared anywhere within a function will be in scope for the *entire function*, regardless of where it is declared. +A variable declared anywhere within a function will be in scope for the *entire function*, regardless of where it is declared. This happens because Solidity inherits its scoping rules from JavaScript. This is in contrast to many languages where variables are only scoped where they are declared until the end of the semantic block. As a result, the following code is illegal and cause the compiler to throw an error, ``Identifier already declared``:: @@ -220,6 +226,8 @@ As a result, the following code is legal, despite being poorly written:: return bar;// returns 5 } +.. index:: ! exception, ! throw + Exceptions ========== diff --git a/docs/types.rst b/docs/types.rst index 1a0de358..50e86ed0 100644 --- a/docs/types.rst +++ b/docs/types.rst @@ -577,7 +577,7 @@ can actually be any type, including mappings. Mappings can be seen as hashtables which are virtually initialized such that every possible key exists and is mapped to a value whose byte-representation is -all zeros. The similarity ends here, though: The key data is not actually stored +all zeros: a type's :ref:`default value <default-value>`. The similarity ends here, though: The key data is not actually stored in a mapping, only its ``sha3`` hash used to look up the value. Because of this, mappings do not have a length or a concept of a key or value being "set". |