Search code examples
blockchainethereumsolidity

Can I use tuple as a key in mapping?


I want to do this

 struct A
    {
        uint x;
        string str;
    }
 mapping ((uint,bytes32,uint)=> structA);

Can use tuple as a key in mapping?


Solution

  • You can't, only elementary types can be used as keys, see the documentation: https://solidity.readthedocs.io/en/v0.5.8/types.html#mapping-types

    You can however convert the tuple to a bytes32 type using the keccak256 hash function and use that value safely as the key, like this:

    keccak256(abi.encodePacked(a, b, c))

    where a, b, c would be the values of your tuple.