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>
This commit is contained in:
Chris Ward 2018-11-15 11:59:37 +01:00 committed by chriseth
parent 0d1dd30ce8
commit 78ca2801d8
2 changed files with 19 additions and 56 deletions

View File

@ -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 ``contractname.kill.sendTransaction({from:eth.coinbase})``, just the same as my
examples. 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? 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? Can a contract pass an array (static size) or string or ``bytes`` (dynamic size) to another contract?
===================================================================================================== =====================================================================================================

View File

@ -465,11 +465,13 @@ a non-rational number).
.. index:: literal, literal;string, string .. index:: literal, literal;string, string
.. _string_literals: .. _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``. 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: String literals support the following escape characters:
- ``\<newline>`` (escapes an actual newline) - ``\<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 / Inline Arrays Array Literals
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^
Array literals are arrays that are written as an expression and are not An array literal is a comma-separated list of one or more expressions, enclosed
assigned to a variable right away. 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 are always statically-sized memory arrays.
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 Fixed size memory arrays cannot be assigned to dynamically-sized memory arrays, i.e. the following is not possible:
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:
:: ::
@ -904,8 +907,8 @@ possible:
} }
} }
It is planned to remove this restriction in the future but currently creates It is planned to remove this restriction in the future, but it creates some
some complications because of how arrays are passed in the ABI. complications because of how arrays are passed in the ABI.
.. index:: ! array;length, length, push, pop, !array;push, !array;pop .. index:: ! array;length, length, push, pop, !array;push, !array;pop