Skip to content

Latest commit

 

History

History
84 lines (67 loc) · 2.98 KB

Mapping Types.md

File metadata and controls

84 lines (67 loc) · 2.98 KB

Mappings define key-value pairs and are declared using the syntax mapping(KeyType => ValueType) VariableName.

  1. The KeyType can be any built-in value type, bytes, string, or any contract or enum type. Other user-defined or complex types, such as mappings, structs or array types are not allowed. ValueType can be any type, including mappings, arrays and structs.

  2. Key data is not stored in a mapping, only its keccak256 hash is used to look up the value

  3. They do not have a length or a concept of a key or value being set

  4. They can only have a data location of storage and thus are allowed for state variables, as storage reference types in functions, or as parameters for library functions

  5. They cannot be used as parameters or return parameters of contract functions that are publicly visible. These restrictions are also true for arrays and structs that contain mappings.

  6. You cannot iterate over mappings, i.e. you cannot enumerate their keys. It is possible, though, to implement a data structure on top of them and iterate over that.


Slide Screenshot

065.jpg


Slide Deck

  • Key-Value Pairs
  • mapping(KeyType => ValueType) VariableName.
  • Key Type -> Restricted
  • Value Type -> Any
  • Key Value -> Not Stored
  • Keccak256 Lookup
  • No Length/Set, Storage Only, No Iteration

References


Solidity by Example

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.3;

contract Mapping {
    // Mapping from address to uint
    mapping(address => uint) public myMap;

    function get(address _addr) public view returns (uint) {
        // Mapping always returns a value.
        // If the value was never set, it will return the default value.
        return myMap[_addr];
    }

    function set(address _addr, uint _i) public {
        // Update the value at this address
        myMap[_addr] = _i;
    }

    function remove(address _addr) public {
        // Reset the value to the default value.
        delete myMap[_addr];
    }
}

contract NestedMapping {
    // Nested mapping (mapping from address to another mapping)
    mapping(address => mapping(uint => bool)) public nested;

    function get(address _addr1, uint _i) public view returns (bool) {
        // You can get values from a nested mapping
        // even when it is not initialized
        return nested[_addr1][_i];
    }

    function set(
        address _addr1,
        uint _i,
        bool _boo
    ) public {
        nested[_addr1][_i] = _boo;
    }

    function remove(address _addr1, uint _i) public {
        delete nested[_addr1][_i];
    }
}