1

I have a struct with a nested mapping which I am instantiating and trying to push to an array, but I'm receiving the following error on the line i try to push:

Storage arrays with nested mappings do not support .push(<arg>).

Its obvious what the compiler is complaining about, my struct has a mapping and im trying to perform a push. Is this fundamentally not allowed or is there something I can change in my implementation to make this work? I'm using solidity 0.8.0.

Code:

struct Dividend {
    mapping(address => bool) claimed;
    uint256 pool;
}
Dividend[] public dividends;

function createDividends() public {
    Dividend storage d;
    d.pool = dividendPool;
    dividends.push(d);
}
1

mappings cannot be copied around through assignment either in storage or memory and can only be referenced. A work around is the following snippet.

struct Dividend {
    mapping(address => bool) claimed;
    uint256 pool;
}
Dividend[] public dividends;

function createDividends() public {
    uint256 idx = dividends.length;
    dividends.push();
    Dividend storage d = dividends[idx];
    d.pool = 10;
    d.claimed[msg.sender] = true;
}

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.