diff options
author | Denton Liu <liu.denton+github@gmail.com> | 2016-05-06 03:10:32 +0800 |
---|---|---|
committer | Denton Liu <liu.denton+github@gmail.com> | 2016-05-06 03:10:32 +0800 |
commit | 88bb63a50700b461ea59f281dee6663dc2d72e23 (patch) | |
tree | 069202ce608c9a4867c3c506800ded72c2d8d3a2 /docs/frequently-asked-questions.rst | |
parent | 3abb987179091b1d5b5fe25c86d928f642411bdb (diff) | |
download | dexon-solidity-88bb63a50700b461ea59f281dee6663dc2d72e23.tar.gz dexon-solidity-88bb63a50700b461ea59f281dee6663dc2d72e23.tar.zst dexon-solidity-88bb63a50700b461ea59f281dee6663dc2d72e23.zip |
Corrected formatting
Diffstat (limited to 'docs/frequently-asked-questions.rst')
-rw-r--r-- | docs/frequently-asked-questions.rst | 46 |
1 files changed, 37 insertions, 9 deletions
diff --git a/docs/frequently-asked-questions.rst b/docs/frequently-asked-questions.rst index d6c54887..ea8b6a67 100644 --- a/docs/frequently-asked-questions.rst +++ b/docs/frequently-asked-questions.rst @@ -184,7 +184,9 @@ An example of this would be:: uint someNumber; string someString; } + mapping(uint => mapping(string => myStruct)) myDynamicMapping; + function storeInMapping() { myDynamicMapping[1]["Foo"] = myStruct(2, "Bar"); } @@ -351,11 +353,17 @@ should be noted that you must declare them as static memory arrays. Examples:: contract C { - struct S { uint a; uint b; } + struct S { + uint a; + uint b; + } + S public x = S(1, 2); string name = "Ada"; string[4] memory AdaArr = ["This", "is", "an", "array"]; } + + contract D { C c = new C(); } @@ -405,9 +413,11 @@ you should always convert it to a `bytes` first:: contract C { string s; + function append(byte c) { bytes(s).push(c); } + function set(uint i, byte c) { bytes(s)[i] = c; } @@ -448,15 +458,21 @@ If you do not want to throw, you can return a pair:: contract C { uint[] counters; + function getCounter(uint index) returns (uint counter, bool error) { if (index >= counters.length) return (0, true); else return (counters[index], false); - } + } + function checkCounter(uint index) { var (counter, error) = getCounter(index); - if (error) { ... } - else { ... } + if (error) { + ... + } + else { + ... + } } } @@ -515,12 +531,15 @@ Example:: contract C { uint[] data1; uint[] data2; + function appendOne() { append(data1); } + function appendTwo() { append(data2); } + function append(uint[] storage d) { d.push(1); } @@ -542,6 +561,7 @@ be created in memory, although it will be created in storage:: contract C { uint someVariable; uint[] data; + function f() { uint[] x; x.push(2); @@ -565,6 +585,7 @@ The correct way to do this is the following:: contract C { uint someVariable; uint[] data; + function f() { uint[] x = data; x.push(2); @@ -674,6 +695,7 @@ This is a very interesting question. Suppose that we have a contract field set u struct user{ mapping(string => address) usedContracts; } + function somefunction{ user user1; user1.usedContracts["Hello"] = "World"; @@ -694,6 +716,7 @@ In this example:: contract B {} contract A { address child; + function test() { child = (new B).value(10)(); //construct a new B with 10 wei } @@ -735,16 +758,20 @@ independent copies will be created:: contract C { uint[20] x; + function f() { g(x); h(x); } + function g(uint[20] y) { y[2] = 3; } + function h(uint[20] storage y) { y[3] = 4; } + } The call to `g(x)` will not have an effect on `x` because it needs to create an independent copy of the storage value in memory @@ -765,10 +792,10 @@ contract level) with `arrayname.length = <some new length>;`. If you get the :: - int8[] memory memArr; // Case 1 - memArr.length++; // illegal - int8[5] storageArr; // Case 2 - somearray.length++; // legal + int8[] memory memArr; // Case 1 + memArr.length++; // illegal + int8[5] storageArr; // Case 2 + somearray.length++; // legal int8[5] storage storageArr2; // Explicit case 2 somearray2.length++; // legal @@ -821,7 +848,8 @@ What does the following strange check do in the Custom Token contract? :: - if (balanceOf[_to] + _value < balanceOf[_to]) throw; + if (balanceOf[_to] + _value < balanceOf[_to]) + throw; Integers in Solidity (and most other machine-related programming languages) are restricted to a certain range. For `uint256`, this is `0` up to `2**256 - 1`. If the result of some operation on those numbers |