1

I'm very new to actual programming not as much blockchain. I know a little Java and am trying to learn solidity I am reading through the Intro to smart contracts turotial and on the 7th line (see below) they declare a variable using mapping but I don't understand how the => comes into play.

mapping (address => uint) public balances;
1
  • I read that piece code as 'mapping from address to uint'.
    – Ismael
    Commented Oct 12, 2017 at 7:18

2 Answers 2

2

mapping is similar to Dictionary in Java where you specify key and value pair like

var mydictionary = new Dictionary(key,value);

while in solidity you use the same with => ; where Key = address and uint as value.

3
  • I see I was just confused as to why it is a => as supposed to a =. But your saying this is nothing math related, its just built into solidity that way?
    – Mbando
    Commented Oct 11, 2017 at 4:50
  • yup...its more of syntax definition in solidity.
    – Rangesh
    Commented Oct 11, 2017 at 5:04
  • 1
    It is an arrow! It indicates the direction of your mapping. That's all :)
    – simonDos
    Commented Oct 11, 2017 at 8:06
3

In an array with key value pair; you can refer to a value using the corresponding key.

i.e. array[key] will return the value stored.

In solidity mapping, can be think of as an array and here in solidity syntax mapping (address => uint) public balances; means that balances has set of uint data type values that are mapped by address data type.

Basically you can get a corresponding uint value for a given address from balances. To get a corresponding value here you need to call balances[address] and that will return the uint value.

You can find a detailed explanation in the solidity docs here.

Mapping types are declared as mapping(_KeyType => _ValueType). Here _KeyType can be almost any type except for a mapping, a dynamically sized array, a contract, an enum and a struct. _ValueType can actually be any type, including mappings.

Mappings can be seen as hash tables which are virtually initialized such that every possible key exists and is mapped to a value whose byte-representation is all zeros: a type’s default value. The similarity ends here, though: The key data is not actually stored in a mapping, only its keccak256 hash used to look up the value.

And this question will also be a good read on how mapping works.

1
  • 1
    Concise explanation. Commented Oct 11, 2019 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.