2

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 Answer 1

3

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; } 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.