0

I have a data structure where:

  1. The smart contract holds an array of structs (a "community") in state
  2. That struct has an array of structs ("delegates")

When I try initialize one of the top level structs (a "community") and push it to the array, I get the error:

UnimplementedFeatureError: Copying of type struct contributor memory[] memory to storage not yet supported.

Code to reproduce this is here:

pragma solidity ^0.8.23;

struct contributor {
    address id;
    string description;
    bool community;
}

struct community {
    address id;
    string description;
    contributor[] delegates;
}

contract communityDelegator {
     community[] public communities;

      // Make a constructor
      constructor() public {
      }

     // Function to add a new community
     function createCommunity(string calldata newDescription) public {

      community memory newCommunity = community({
         id: msg.sender,
         description: newDescription,
         delegates:  new contributor[](0) // Getting rid of this and changing the community type signature works
      });

      communities.push(newCommunity);

     }

}

I get that if I have a struct in memory, to copy to storage I'd have to loop over every element (example). But there's nothing to loop over because I'm just trying to initialize it.

1 Answer 1

0

To fix the UnimplementedFeatureError in Solidity, first, create an empty array of contributors in memory. Then, initialize the struct's delegates array with this empty array inside the function. By doing this, you avoid directly assigning an empty memory array to a storage array, allowing successful struct initialization and array population. Finally, push the initialized struct into the storage array.

Code Snippet:

Contributor[] memory emptyContributors;
Community memory newCommunity = Community({
    id: msg.sender,
    description: newDescription,
    delegates: emptyContributors
});
communities.push(newCommunity);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.