Corrected formatting

This commit is contained in:
Denton Liu 2016-05-05 15:10:32 -04:00
parent 3abb987179
commit 88bb63a507

View File

@ -184,7 +184,9 @@ An example of this would be::
uint someNumber; uint someNumber;
string someString; string someString;
} }
mapping(uint => mapping(string => myStruct)) myDynamicMapping; mapping(uint => mapping(string => myStruct)) myDynamicMapping;
function storeInMapping() { function storeInMapping() {
myDynamicMapping[1]["Foo"] = myStruct(2, "Bar"); myDynamicMapping[1]["Foo"] = myStruct(2, "Bar");
} }
@ -351,11 +353,17 @@ should be noted that you must declare them as static memory arrays.
Examples:: Examples::
contract C { contract C {
struct S { uint a; uint b; } struct S {
uint a;
uint b;
}
S public x = S(1, 2); S public x = S(1, 2);
string name = "Ada"; string name = "Ada";
string[4] memory AdaArr = ["This", "is", "an", "array"]; string[4] memory AdaArr = ["This", "is", "an", "array"];
} }
contract D { contract D {
C c = new C(); C c = new C();
} }
@ -405,9 +413,11 @@ you should always convert it to a `bytes` first::
contract C { contract C {
string s; string s;
function append(byte c) { function append(byte c) {
bytes(s).push(c); bytes(s).push(c);
} }
function set(uint i, byte c) { function set(uint i, byte c) {
bytes(s)[i] = c; bytes(s)[i] = c;
} }
@ -448,15 +458,21 @@ If you do not want to throw, you can return a pair::
contract C { contract C {
uint[] counters; uint[] counters;
function getCounter(uint index) function getCounter(uint index)
returns (uint counter, bool error) { returns (uint counter, bool error) {
if (index >= counters.length) return (0, true); if (index >= counters.length) return (0, true);
else return (counters[index], false); else return (counters[index], false);
} }
function checkCounter(uint index) { function checkCounter(uint index) {
var (counter, error) = getCounter(index); var (counter, error) = getCounter(index);
if (error) { ... } if (error) {
else { ... } ...
}
else {
...
}
} }
} }
@ -515,12 +531,15 @@ Example::
contract C { contract C {
uint[] data1; uint[] data1;
uint[] data2; uint[] data2;
function appendOne() { function appendOne() {
append(data1); append(data1);
} }
function appendTwo() { function appendTwo() {
append(data2); append(data2);
} }
function append(uint[] storage d) { function append(uint[] storage d) {
d.push(1); d.push(1);
} }
@ -542,6 +561,7 @@ be created in memory, although it will be created in storage::
contract C { contract C {
uint someVariable; uint someVariable;
uint[] data; uint[] data;
function f() { function f() {
uint[] x; uint[] x;
x.push(2); x.push(2);
@ -565,6 +585,7 @@ The correct way to do this is the following::
contract C { contract C {
uint someVariable; uint someVariable;
uint[] data; uint[] data;
function f() { function f() {
uint[] x = data; uint[] x = data;
x.push(2); x.push(2);
@ -674,6 +695,7 @@ This is a very interesting question. Suppose that we have a contract field set u
struct user{ struct user{
mapping(string => address) usedContracts; mapping(string => address) usedContracts;
} }
function somefunction{ function somefunction{
user user1; user user1;
user1.usedContracts["Hello"] = "World"; user1.usedContracts["Hello"] = "World";
@ -694,6 +716,7 @@ In this example::
contract B {} contract B {}
contract A { contract A {
address child; address child;
function test() { function test() {
child = (new B).value(10)(); //construct a new B with 10 wei child = (new B).value(10)(); //construct a new B with 10 wei
} }
@ -735,16 +758,20 @@ independent copies will be created::
contract C { contract C {
uint[20] x; uint[20] x;
function f() { function f() {
g(x); g(x);
h(x); h(x);
} }
function g(uint[20] y) { function g(uint[20] y) {
y[2] = 3; y[2] = 3;
} }
function h(uint[20] storage y) { function h(uint[20] storage y) {
y[3] = 4; y[3] = 4;
} }
}
The call to `g(x)` will not have an effect on `x` because it needs 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 to create an independent copy of the storage value in memory
@ -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. 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 For `uint256`, this is `0` up to `2**256 - 1`. If the result of some operation on those numbers