Mapping in Structs

I have been developing with Solidity < 7.0, and with a new project, I want to switch to the latest version.

However, I noticed one thing reading through breaking changes that I am not sure what the “new best practice” would be.

This is regarding that struct that contains mappings can only be utilized in storage mode now.

With data, it’s normally best to group related information if possible. Though with this change, I am not sure if it is a good idea to add a mapping to a struct, even if it makes sense data-wise.

When considering the cost of interaction with a smart contract, and that if you utilize mapping it will prevent you from using it as memory.

Does this mean that it would be better to utilize double mappings instead of storing a mapping inside a struct?

Old Code Example:

struct Account {
    uint id;
    address sponsor;
    mapping(uint => Data) additionalData;
  }

struct Data {
    uint8 info;
    uint8 etc;
}

mapping(address => Account) public account;

In the above example, would it make sense now to make this:

struct Account {
    uint id;
    address sponsor;
  }

struct Data {
    uint8 info;
    uint8 etc;
}

mapping(address => Account) public account;
mapping(address => mapping(uint => Data)) internal data;

Or does it even with the new restrictions, make sense to keep mappings in the structs?

1 Like

Hi @Sven,

My reading of the Solidity documentation was that you can keep the code as is as you are using the struct mapping in storage.
https://docs.soliditylang.org/en/v0.8.4/070-breaking-changes.html#mappings-outside-storage