diff options
Diffstat (limited to 'docs/frequently-asked-questions.rst')
-rw-r--r-- | docs/frequently-asked-questions.rst | 145 |
1 files changed, 0 insertions, 145 deletions
diff --git a/docs/frequently-asked-questions.rst b/docs/frequently-asked-questions.rst index 8b655b0d..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? =========================================================================================== @@ -143,44 +125,6 @@ arguments for you. See `ping.sol <https://github.com/fivedogit/solidity-baby-steps/blob/master/contracts/45_ping.sol>`_ and `pong.sol <https://github.com/fivedogit/solidity-baby-steps/blob/master/contracts/45_pong.sol>`_. -When returning a value of say ``uint`` type, is it possible to return an ``undefined`` or "null"-like value? -============================================================================================================ - -This is not possible, because all types use up the full value range. - -You have the option to ``throw`` on error, which will also revert the whole -transaction, which might be a good idea if you ran into an unexpected -situation. - -If you do not want to throw, you can return a pair:: - - pragma solidity >0.4.23 <0.6.0; - - contract C { - uint[] counters; - - function getCounter(uint index) - public - view - returns (uint counter, bool error) { - if (index >= counters.length) - return (0, true); - else - return (counters[index], false); - } - - function checkCounter(uint index) public view { - (uint counter, bool error) = getCounter(index); - if (error) { - // Handle the error - } else { - // Do something with counter. - require(counter > 7, "Invalid counter value"); - } - } - } - - Are comments included with deployed contracts and do they increase deployment gas? ================================================================================== @@ -232,24 +176,6 @@ Note2: Optimizing storage access can pull the gas costs down considerably, becau currently do not work across loops and also have a problem with bounds checking. You might get much better results in the future, though. -What happens to a ``struct``'s mapping when copying over a ``struct``? -====================================================================== - -This is a very interesting question. Suppose that we have a contract field set up like such:: - - struct User { - mapping(string => string) comments; - } - - function somefunction public { - User user1; - user1.comments["Hello"] = "World"; - User user2 = user1; - } - -In this case, the mapping of the struct being copied over into ``user2`` is ignored as there is no "list of mapped keys". -Therefore it is not possible to find out which values should be copied over. - How do I initialize a contract with only a specific amount of wei? ================================================================== @@ -273,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? ===================================================================================================== @@ -325,55 +229,6 @@ to create an independent copy of the storage value in memory. On the other hand, ``h(x)`` successfully modifies ``x`` because only a reference and not a copy is passed. -Sometimes, when I try to change the length of an array with ex: ``arrayname.length = 7;`` I get a compiler error ``Value must be an lvalue``. Why? -================================================================================================================================================== - -You can resize a dynamic array in storage (i.e. an array declared at the -contract level) with ``arrayname.length = <some new length>;``. If you get the -"lvalue" error, you are probably doing one of two things wrong. - -1. You might be trying to resize an array in "memory", or - -2. You might be trying to resize a non-dynamic array. - -:: - - pragma solidity >=0.4.18 <0.6.0; - - // This will not compile - contract C { - int8[] dynamicStorageArray; - int8[5] fixedStorageArray; - - function f() public { - int8[] memory memArr; // Case 1 - memArr.length++; // illegal - - int8[5] storage storageArr = fixedStorageArray; // Case 2 - storageArr.length++; // illegal - - int8[] storage storageArr2 = dynamicStorageArray; - storageArr2.length++; // legal - - - } - } - -**Important note:** In Solidity, array dimensions are declared backwards from the way you -might be used to declaring them in C or Java, but they are access as in -C or Java. - -For example, ``int8[][5] somearray;`` are 5 dynamic ``int8`` arrays. - -The reason for this is that ``T[5]`` is always an array of 5 ``T``'s, -no matter whether ``T`` itself is an array or not (this is not the -case in C or Java). - -Is it possible to return an array of strings (``string[]``) from a Solidity function? -===================================================================================== - -Only when ``pragma experimental "ABIEncoderV2";`` is used. - What does the following strange check do in the Custom Token contract? ====================================================================== |