diff options
author | Denton Liu <liu.denton+github@gmail.com> | 2016-05-18 23:05:28 +0800 |
---|---|---|
committer | Denton Liu <liu.denton+github@gmail.com> | 2016-05-18 23:35:32 +0800 |
commit | 7c22a387f34d5cbc92b6b6ed78c281a480ba7739 (patch) | |
tree | 8300e1a97cc6537ff463ef705a5b28b313cfb6a0 /docs/types.rst | |
parent | ff26ea6c08a400192c5a5fff581a7ce649f717bd (diff) | |
download | dexon-solidity-7c22a387f34d5cbc92b6b6ed78c281a480ba7739.tar.gz dexon-solidity-7c22a387f34d5cbc92b6b6ed78c281a480ba7739.tar.zst dexon-solidity-7c22a387f34d5cbc92b6b6ed78c281a480ba7739.zip |
Changed whitespace formatting
Diffstat (limited to 'docs/types.rst')
-rw-r--r-- | docs/types.rst | 258 |
1 files changed, 137 insertions, 121 deletions
diff --git a/docs/types.rst b/docs/types.rst index bd972bef..4beea9e0 100644 --- a/docs/types.rst +++ b/docs/types.rst @@ -171,21 +171,21 @@ to and from all integer types but implicit conversion is not allowed. enum ActionChoices { GoLeft, GoRight, GoStraight, SitStill } ActionChoices choice; ActionChoices constant defaultChoice = ActionChoices.GoStraight; - function setGoStraight() - { + + function setGoStraight() { choice = ActionChoices.GoStraight; } + // Since enum types are not part of the ABI, the signature of "getChoice" // will automatically be changed to "getChoice() returns (uint8)" // for all matters external to Solidity. The integer type used is just // large enough to hold all enum values, i.e. if you have more values, // `uint16` will be used and so on. - function getChoice() returns (ActionChoices) - { + function getChoice() returns (ActionChoices) { return choice; } - function getDefaultChoice() returns (uint) - { + + function getDefaultChoice() returns (uint) { return uint(defaultChoice); } } @@ -226,26 +226,28 @@ memory-stored reference type does not create a copy. :: - contract c { - uint[] x; // the data location of x is storage - // the data location of memoryArray is memory - function f(uint[] memoryArray) { - x = memoryArray; // works, copies the whole array to storage - var y = x; // works, assigns a pointer, data location of y is storage - y[7]; // fine, returns the 8th element - y.length = 2; // fine, modifies x through y - delete x; // fine, clears the array, also modifies y - // The following does not work; it would need to create a new temporary / - // unnamed array in storage, but storage is "statically" allocated: - // y = memoryArray; - // This does not work either, since it would "reset" the pointer, but there - // is no sensible location it could point to. - // delete y; - g(x); // calls g, handing over a reference to x - h(x); // calls h and creates an independent, temporary copy in memory - } - function g(uint[] storage storageArray) internal {} - function h(uint[] memoryArray) {} + contract C { + uint[] x; // the data location of x is storage + + // the data location of memoryArray is memory + function f(uint[] memoryArray) { + x = memoryArray; // works, copies the whole array to storage + var y = x; // works, assigns a pointer, data location of y is storage + y[7]; // fine, returns the 8th element + y.length = 2; // fine, modifies x through y + delete x; // fine, clears the array, also modifies y + // The following does not work; it would need to create a new temporary / + // unnamed array in storage, but storage is "statically" allocated: + // y = memoryArray; + // This does not work either, since it would "reset" the pointer, but there + // is no sensible location it could point to. + // delete y; + g(x); // calls g, handing over a reference to x + h(x); // calls h and creates an independent, temporary copy in memory + } + + function g(uint[] storage storageArray) internal {} + function h(uint[] memoryArray) {} } Summary @@ -303,12 +305,12 @@ the `.length` member. :: contract C { - function f(uint len) { - uint[] memory a = new uint[](7); - bytes memory b = new bytes(len); - // Here we have a.length == 7 and b.length == len - a[6] = 8; - } + function f(uint len) { + uint[] memory a = new uint[](7); + bytes memory b = new bytes(len); + // Here we have a.length == 7 and b.length == len + a[6] = 8; + } } @@ -339,51 +341,59 @@ Members :: contract ArrayContract { - uint[2**20] m_aLotOfIntegers; - // Note that the following is not a pair of arrays but an array of pairs. - bool[2][] m_pairsOfFlags; - // newPairs is stored in memory - the default for function arguments - function setAllFlagPairs(bool[2][] newPairs) { - // assignment to a storage array replaces the complete array - m_pairsOfFlags = newPairs; - } - function setFlagPair(uint index, bool flagA, bool flagB) { - // access to a non-existing index will throw an exception - m_pairsOfFlags[index][0] = flagA; - m_pairsOfFlags[index][1] = flagB; - } - function changeFlagArraySize(uint newSize) { - // if the new size is smaller, removed array elements will be cleared - m_pairsOfFlags.length = newSize; - } - function clear() { - // these clear the arrays completely - delete m_pairsOfFlags; - delete m_aLotOfIntegers; - // identical effect here - m_pairsOfFlags.length = 0; - } - bytes m_byteData; - function byteArrays(bytes data) { - // byte arrays ("bytes") are different as they are stored without padding, - // but can be treated identical to "uint8[]" - m_byteData = data; - m_byteData.length += 7; - m_byteData[3] = 8; - delete m_byteData[2]; - } - function addFlag(bool[2] flag) returns (uint) { - return m_pairsOfFlags.push(flag); - } - function createMemoryArray(uint size) returns (bytes) { - // Dynamic memory arrays are created using `new`: - uint[2][] memory arrayOfPairs = new uint[2][](size); - // Create a dynamic byte array: - bytes memory b = new bytes(200); - for (uint i = 0; i < b.length; i++) - b[i] = byte(i); - return b; - } + uint[2**20] m_aLotOfIntegers; + // Note that the following is not a pair of arrays but an array of pairs. + bool[2][] m_pairsOfFlags; + // newPairs is stored in memory - the default for function arguments + + function setAllFlagPairs(bool[2][] newPairs) { + // assignment to a storage array replaces the complete array + m_pairsOfFlags = newPairs; + } + + function setFlagPair(uint index, bool flagA, bool flagB) { + // access to a non-existing index will throw an exception + m_pairsOfFlags[index][0] = flagA; + m_pairsOfFlags[index][1] = flagB; + } + + function changeFlagArraySize(uint newSize) { + // if the new size is smaller, removed array elements will be cleared + m_pairsOfFlags.length = newSize; + } + + function clear() { + // these clear the arrays completely + delete m_pairsOfFlags; + delete m_aLotOfIntegers; + // identical effect here + m_pairsOfFlags.length = 0; + } + + bytes m_byteData; + + function byteArrays(bytes data) { + // byte arrays ("bytes") are different as they are stored without padding, + // but can be treated identical to "uint8[]" + m_byteData = data; + m_byteData.length += 7; + m_byteData[3] = 8; + delete m_byteData[2]; + } + + function addFlag(bool[2] flag) returns (uint) { + return m_pairsOfFlags.push(flag); + } + + function createMemoryArray(uint size) returns (bytes) { + // Dynamic memory arrays are created using `new`: + uint[2][] memory arrayOfPairs = new uint[2][](size); + // Create a dynamic byte array: + bytes memory b = new bytes(200); + for (uint i = 0; i < b.length; i++) + b[i] = byte(i); + return b; + } } @@ -400,41 +410,46 @@ shown in the following example: :: contract CrowdFunding { - // Defines a new type with two fields. - struct Funder { - address addr; - uint amount; - } - struct Campaign { - address beneficiary; - uint fundingGoal; - uint numFunders; - uint amount; - mapping (uint => Funder) funders; - } - uint numCampaigns; - mapping (uint => Campaign) campaigns; - function newCampaign(address beneficiary, uint goal) returns (uint campaignID) { - campaignID = numCampaigns++; // campaignID is return variable - // Creates new struct and saves in storage. We leave out the mapping type. - campaigns[campaignID] = Campaign(beneficiary, goal, 0, 0); - } - function contribute(uint campaignID) { - Campaign c = campaigns[campaignID]; + // Defines a new type with two fields. + struct Funder { + address addr; + uint amount; + } + + struct Campaign { + address beneficiary; + uint fundingGoal; + uint numFunders; + uint amount; + mapping (uint => Funder) funders; + } + + uint numCampaigns; + mapping (uint => Campaign) campaigns; + + function newCampaign(address beneficiary, uint goal) returns (uint campaignID) { + campaignID = numCampaigns++; // campaignID is return variable + // Creates new struct and saves in storage. We leave out the mapping type. + campaigns[campaignID] = Campaign(beneficiary, goal, 0, 0); + } + + function contribute(uint campaignID) { + Campaign c = campaigns[campaignID]; // Creates a new temporary memory struct, initialised with the given values // and copies it over to storage. // Note that you can also use Funder(msg.sender, msg.value) to initialise. - c.funders[c.numFunders++] = Funder({addr: msg.sender, amount: msg.value}); - c.amount += msg.value; - } - function checkGoalReached(uint campaignID) returns (bool reached) { - Campaign c = campaigns[campaignID]; - if (c.amount < c.fundingGoal) - return false; - c.beneficiary.send(c.amount); - c.amount = 0; - return true; - } + c.funders[c.numFunders++] = Funder({addr: msg.sender, amount: msg.value}); + c.amount += msg.value; + } + + function checkGoalReached(uint campaignID) returns (bool reached) { + Campaign c = campaigns[campaignID]; + if (c.amount < c.fundingGoal) + return false; + c.beneficiary.send(c.amount); + c.amount = 0; + return true; + } } The contract does not provide the full functionality of a crowdfunding @@ -495,18 +510,19 @@ It is important to note that `delete a` really behaves like an assignment to `a` :: contract DeleteExample { - uint data; - uint[] dataArray; - function f() { - uint x = data; - delete x; // sets x to 0, does not affect data - delete data; // sets data to 0, does not affect x which still holds a copy - uint[] y = dataArray; - delete dataArray; // this sets dataArray.length to zero, but as uint[] is a complex object, also - // y is affected which is an alias to the storage object - // On the other hand: "delete y" is not valid, as assignments to local variables - // referencing storage objects can only be made from existing storage objects. - } + uint data; + uint[] dataArray; + + function f() { + uint x = data; + delete x; // sets x to 0, does not affect data + delete data; // sets data to 0, does not affect x which still holds a copy + uint[] y = dataArray; + delete dataArray; // this sets dataArray.length to zero, but as uint[] is a complex object, also + // y is affected which is an alias to the storage object + // On the other hand: "delete y" is not valid, as assignments to local variables + // referencing storage objects can only be made from existing storage objects. + } } .. index:: ! type;conversion, ! cast |