diff options
author | Alex Beregszaszi <alex@rtfs.hu> | 2016-10-16 05:50:46 +0800 |
---|---|---|
committer | Alex Beregszaszi <alex@rtfs.hu> | 2016-10-19 21:02:50 +0800 |
commit | fcba4d927ced8410fdbcd8f9863741bf20e47ba8 (patch) | |
tree | bb7ef692ad3a4fa85bbf995bff2c378c75f1e584 /docs | |
parent | 1b3713742f10a4749e6f15b9344d8ffaef19790e (diff) | |
download | dexon-solidity-fcba4d927ced8410fdbcd8f9863741bf20e47ba8.tar dexon-solidity-fcba4d927ced8410fdbcd8f9863741bf20e47ba8.tar.gz dexon-solidity-fcba4d927ced8410fdbcd8f9863741bf20e47ba8.tar.bz2 dexon-solidity-fcba4d927ced8410fdbcd8f9863741bf20e47ba8.tar.lz dexon-solidity-fcba4d927ced8410fdbcd8f9863741bf20e47ba8.tar.xz dexon-solidity-fcba4d927ced8410fdbcd8f9863741bf20e47ba8.tar.zst dexon-solidity-fcba4d927ced8410fdbcd8f9863741bf20e47ba8.zip |
Merge the documentation about mappings
Diffstat (limited to 'docs')
-rw-r--r-- | docs/frequently-asked-questions.rst | 46 | ||||
-rw-r--r-- | docs/types.rst | 27 |
2 files changed, 27 insertions, 46 deletions
diff --git a/docs/frequently-asked-questions.rst b/docs/frequently-asked-questions.rst index 6d47e4a7..353d8abc 100644 --- a/docs/frequently-asked-questions.rst +++ b/docs/frequently-asked-questions.rst @@ -104,52 +104,6 @@ This example demonstrates how to send funds from a contract to an address. See `endowment_retriever <https://github.com/fivedogit/solidity-baby-steps/blob/master/contracts/30_endowment_retriever.sol>`_. -What is a ``mapping`` and how do we use them? -============================================= - -A mapping is very similar to a K->V hashmap. -If you have a state variable of type ``mapping (string -> uint) x;``, then you can -access the value by ``x["somekeystring"]``. - -How can I get the length of a ``mapping``? -========================================== - -Mappings are a rather low-level data structure. It does not store the keys -and it is not possible to know which or how many values are "set". Actually, -all values to all possible keys are set by default, they are just -initialised with the zero value. - -In this sense, the attribute ``length`` for a mapping does not really apply. - -If you want to have a "sized mapping", you can use the iterable mapping -(see below) or just a dynamically-sized array of structs. - -Are ``mapping``'s iterable? -=========================== - -Mappings themselves are not iterable, but you can use a higher-level -datastructure on top of it, for example the `iterable mapping <https://github.com/ethereum/dapp-bin/blob/master/library/iterable_mapping.sol>`_. - -Can I put arrays inside of a ``mapping``? How do I make a ``mapping`` of a ``mapping``? -======================================================================================= - -Mappings are already syntactically similar to arrays as they are, therefore it doesn't make much sense to store an array in them. Rather what you should do is create a mapping of a mapping. - -An example of this would be:: - - contract C { - struct myStruct { - uint someNumber; - string someString; - } - - mapping(uint => mapping(string => myStruct)) myDynamicMapping; - - function storeInMapping() { - myDynamicMapping[1]["Foo"] = myStruct(2, "Bar"); - } - } - Can you return an array or a ``string`` from a solidity function call? ====================================================================== diff --git a/docs/types.rst b/docs/types.rst index 5fb512d5..959d4de3 100644 --- a/docs/types.rst +++ b/docs/types.rst @@ -617,6 +617,33 @@ Because of this, mappings do not have a length or a concept of a key or value be Mappings are only allowed for state variables (or as storage reference types in internal functions). +It is possible to mark mappings ``public`` and have Solidity create an accessor. +The ``_KeyType`` will become a required parameter for the accessor and it will +return ``_ValueType``. + +:: + + pragma solidity ^0.4.0; + + contract MappingExample { + mapping(address => uint) public balances; + + function update(uint newBalance) { + balances[msg.sender] = newBalance; + } + } + + contract MappingUser { + function f() returns (uint) { + return MappingExample(<address>).balances(this); + } + } + + +.. note:: + Mappings are not iterable, but it is possible to implement a data structure on top of them. + For an example, see `iterable mapping <https://github.com/ethereum/dapp-bin/blob/master/library/iterable_mapping.sol>`_. + .. index:: assignment, ! delete, lvalue Operators Involving LValues |