diff options
author | Chris Ward <chriswhward@gmail.com> | 2018-11-15 18:59:37 +0800 |
---|---|---|
committer | chriseth <chris@ethereum.org> | 2018-12-03 18:49:42 +0800 |
commit | 78ca2801d8a63769bbd8cff5948c4b92d693d11c (patch) | |
tree | 63179e5cc7a4d2916864d5d1d851ad7db28bc3d0 | |
parent | 0d1dd30ce84e77866e648219f4c84129fcb497e6 (diff) | |
download | dexon-solidity-78ca2801d8a63769bbd8cff5948c4b92d693d11c.tar.gz dexon-solidity-78ca2801d8a63769bbd8cff5948c4b92d693d11c.tar.zst dexon-solidity-78ca2801d8a63769bbd8cff5948c4b92d693d11c.zip |
Move String literal and inline array FAQ items
Fix tab
Update docs/types.rst
Co-Authored-By: ChrisChinchilla <chriswhward@gmail.com>
Update docs/types.rst
Co-Authored-By: ChrisChinchilla <chriswhward@gmail.com>
-rw-r--r-- | docs/frequently-asked-questions.rst | 40 | ||||
-rw-r--r-- | docs/types.rst | 35 |
2 files changed, 19 insertions, 56 deletions
diff --git a/docs/frequently-asked-questions.rst b/docs/frequently-asked-questions.rst index bcdfdcd8..f3c5b1f7 100644 --- a/docs/frequently-asked-questions.rst +++ b/docs/frequently-asked-questions.rst @@ -38,24 +38,6 @@ has it (which includes `Remix <https://remix.ethereum.org/>`_), then ``contractname.kill.sendTransaction({from:eth.coinbase})``, just the same as my examples. -Is it possible to in-line initialize an array like so: ``string[] myarray = ["a", "b"];`` -========================================================================================= - -Yes. However it should be noted that this currently only works with statically sized memory arrays. You can even create an inline memory -array in the return statement. - -Example:: - - pragma solidity >=0.4.16 <0.6.0; - - contract C { - function f() public pure returns (uint8[5] memory) { - string[4] memory adaArr = ["This", "is", "an", "array"]; - adaArr[0] = "That"; - return [1, 2, 3, 4, 5]; - } - } - If I return an ``enum``, I only get integer values in web3.js. How to get the named values? =========================================================================================== @@ -217,28 +199,6 @@ In this example:: } } -What is the relationship between ``bytes32`` and ``string``? Why is it that ``bytes32 somevar = "stringliteral";`` works and what does the saved 32-byte hex value mean? -======================================================================================================================================================================== - -The type ``bytes32`` can hold 32 (raw) bytes. In the assignment ``bytes32 samevar = "stringliteral";``, -the string literal is interpreted in its raw byte form and if you inspect ``somevar`` and -see a 32-byte hex value, this is just ``"stringliteral"`` in hex. - -The type ``bytes`` is similar, only that it can change its length. - -Finally, ``string`` is basically identical to ``bytes`` only that it is assumed -to hold the UTF-8 encoding of a real string. Since ``string`` stores the -data in UTF-8 encoding it is quite expensive to compute the number of -characters in the string (the encoding of some characters takes more -than a single byte). Because of that, ``string s; s.length`` is not yet -supported and not even index access ``s[2]``. But if you want to access -the low-level byte encoding of the string, you can use -``bytes(s).length`` and ``bytes(s)[2]`` which will result in the number -of bytes in the UTF-8 encoding of the string (not the number of -characters) and the second byte (not character) of the UTF-8 encoded -string, respectively. - - Can a contract pass an array (static size) or string or ``bytes`` (dynamic size) to another contract? ===================================================================================================== diff --git a/docs/types.rst b/docs/types.rst index 69c846a6..f67a6d1a 100644 --- a/docs/types.rst +++ b/docs/types.rst @@ -465,11 +465,13 @@ a non-rational number). .. index:: literal, literal;string, string .. _string_literals: -String Literals ---------------- +String Literals and Types +------------------------- String literals are written with either double or single-quotes (``"foo"`` or ``'bar'``). They do not imply trailing zeroes as in C; ``"foo"`` represents three bytes, not four. As with integer literals, their type can vary, but they are implicitly convertible to ``bytes1``, ..., ``bytes32``, if they fit, to ``bytes`` and to ``string``. +For example, with ``bytes32 samevar = "stringliteral"`` the string literal is interpreted in its raw byte form when assigned to a ``bytes32`` type. + String literals support the following escape characters: - ``\<newline>`` (escapes an actual newline) @@ -862,13 +864,20 @@ or create a new memory array and copy every element. } } -.. index:: ! array;literals, !inline;arrays +.. index:: ! array;literals, ! inline;arrays + +Array Literals +^^^^^^^^^^^^^^ + +An array literal is a comma-separated list of one or more expressions, enclosed +in square brackets (``[...]``). For example ``[1, a, f(3)]``. There must be a +common type all elements can be implicitly converted to. This is the elementary +type of the array. -Array Literals / Inline Arrays -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +Array literals are always statically-sized memory arrays. -Array literals are arrays that are written as an expression and are not -assigned to a variable right away. +In the example below, the type of ``[1, 2, 3]`` is +``uint8[3] memory``. Because the type of each of these constants is ``uint8``, if you want the result to be a ``uint[3] memory`` type, you need to convert the first element to ``uint``. :: @@ -883,13 +892,7 @@ assigned to a variable right away. } } -The type of an array literal is a memory array of fixed size whose base -type is the common type of the given elements. The type of ``[1, 2, 3]`` is -``uint8[3] memory``, because the type of each of these constants is ``uint8``. -Because of that, it is necessary to convert the first element in the example -above to ``uint``. Note that currently, fixed size memory arrays cannot -be assigned to dynamically-sized memory arrays, i.e. the following is not -possible: +Fixed size memory arrays cannot be assigned to dynamically-sized memory arrays, i.e. the following is not possible: :: @@ -904,8 +907,8 @@ possible: } } -It is planned to remove this restriction in the future but currently creates -some complications because of how arrays are passed in the ABI. +It is planned to remove this restriction in the future, but it creates some +complications because of how arrays are passed in the ABI. .. index:: ! array;length, length, push, pop, !array;push, !array;pop |