From 0db67b88957c132aeda099d071faee55dbd6b616 Mon Sep 17 00:00:00 2001 From: Denton Liu Date: Wed, 22 Jun 2016 13:25:26 -0400 Subject: Add section about default values of variables --- docs/control-structures.rst | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'docs') diff --git a/docs/control-structures.rst b/docs/control-structures.rst index 2f867cb0..fc55f8e6 100644 --- a/docs/control-structures.rst +++ b/docs/control-structures.rst @@ -151,9 +151,17 @@ Assigning *to* a state variable always creates an independent copy. On the other Scoping and Declarations ======================== -.. index:: ! scoping, ! declarations +.. index:: ! scoping, ! declarations, ! default-value -In Solidity, a variable declared anywhere within a function will be in scope for the *entire function*, regardless of where it is declared. +In Solidity, a variable which is declared is automatically assigned its default value. It will be assigned on contract +initialization if it is a contract-level variable or at the beginning of a function call if it is a local variable. +This is because the EVM must run deterministically so it would be inappropriate to initialize any variables to random garbage values. +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 ``bytes``, each individual +element will be initialized to the default value corresponding to its type. Finally, for dynamically-sized arrays, ``bytes`` +and ``strings``, the default value is a zero-length member of its respective type. + +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``:: -- cgit From cb5fc2301419ea3415a2220775381a70df95ddee Mon Sep 17 00:00:00 2001 From: Denton Liu Date: Mon, 4 Jul 2016 10:51:02 -0400 Subject: Fix incorrect directives --- docs/control-structures.rst | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'docs') diff --git a/docs/control-structures.rst b/docs/control-structures.rst index fc55f8e6..96768919 100644 --- a/docs/control-structures.rst +++ b/docs/control-structures.rst @@ -146,13 +146,13 @@ 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, ! default-value - In Solidity, a variable which is declared is automatically assigned its default value. It will be assigned on contract initialization if it is a contract-level variable or at the beginning of a function call if it is a local variable. This is because the EVM must run deterministically so it would be inappropriate to initialize any variables to random garbage values. @@ -213,6 +213,8 @@ As a result, the following code is legal, despite being poorly written:: return bar;// returns 5 } +.. index:: ! exception, ! throw + Exceptions ========== -- cgit From d4d7d9854a16ae47dba9537029e59191f742fd40 Mon Sep 17 00:00:00 2001 From: Denton Liu Date: Mon, 4 Jul 2016 10:52:08 -0400 Subject: Add reference to default values in mapping section --- docs/types.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'docs') diff --git a/docs/types.rst b/docs/types.rst index dbbd84d3..d7387ab3 100644 --- a/docs/types.rst +++ b/docs/types.rst @@ -570,7 +570,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 `. 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". -- cgit From 14f57568dcee8b2fae60f94d4b77986708c06667 Mon Sep 17 00:00:00 2001 From: Denton Liu Date: Tue, 5 Jul 2016 11:44:39 -0400 Subject: Apply fixes --- docs/control-structures.rst | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'docs') diff --git a/docs/control-structures.rst b/docs/control-structures.rst index 96768919..8893a8d9 100644 --- a/docs/control-structures.rst +++ b/docs/control-structures.rst @@ -153,13 +153,12 @@ Assigning *to* a state variable always creates an independent copy. On the other Scoping and Declarations ======================== -In Solidity, a variable which is declared is automatically assigned its default value. It will be assigned on contract -initialization if it is a contract-level variable or at the beginning of a function call if it is a local variable. -This is because the EVM must run deterministically so it would be inappropriate to initialize any variables to random garbage values. +A variable which is declared will have an initial default value whose byte-representation is all zeros. +This is because the EVM must run without undefined behaviour so it would be inappropriate to initialize any variables to random garbage values. 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 ``bytes``, each individual +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 ``strings``, the default value is a zero-length member of its respective type. +and ``string``, the default value is an empty array or a zero-length member of its respective type. 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. -- cgit From cf0579a86eede7cf9b57695f5fa6557c65d4f5a8 Mon Sep 17 00:00:00 2001 From: Denton Liu Date: Fri, 8 Jul 2016 14:07:58 -0400 Subject: Incorporate changes --- docs/control-structures.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'docs') diff --git a/docs/control-structures.rst b/docs/control-structures.rst index 8893a8d9..6d334ca2 100644 --- a/docs/control-structures.rst +++ b/docs/control-structures.rst @@ -154,11 +154,10 @@ Scoping and Declarations ======================== A variable which is declared will have an initial default value whose byte-representation is all zeros. -This is because the EVM must run without undefined behaviour so it would be inappropriate to initialize any variables to random garbage values. 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 a zero-length member of its respective type. +and ``string``, the default value is an empty array or string. 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. -- cgit