0

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;
  • I read that piece code as 'mapping from address to uint'. – Ismael Oct 12 '17 at 7:18
1

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.

| improve this answer | |
  • 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 Oct 11 '17 at 4:50
  • yup...its more of syntax definition in solidity. – Rangesh Oct 11 '17 at 5:04
  • 1
    It is an arrow! It indicates the direction of your mapping. That's all :) – simonDos Oct 11 '17 at 8:06
2

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.

| improve this answer | |
  • 1
    Concise explanation. – Vishwa Ratna Oct 11 '19 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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