0
Collection[] public collections;

struct Book {
    string name;
    address writer;
    bytes32 proof;
}

struct Collection {
    Book[] books;
    address collectionCreator;
}

// Question: how to push a new collection?
function newCollection() public {
    collections.append(Collection({
        collectionCreator: msg.sender,
        // ISSUE HERE
        books: <HELP>
    }))
}

How to instantiate this new array of a struct inside a struct?

  • In all honesty, this seems really confusing to implement, you could look at using alternatives, like using a mapping instead. – GrandFleet Jan 14 '19 at 4:52
2

I think the easiest thing to do is as follows:

function newCollection() public {
    // Increase the array length first
    collections.length += 1;

    // Get a reference to the new element
    Collection storage collection = collections[collections.length - 1];

    // Set its members as needed        
    collection.collectionCreator = msg.sender;
    // collection.books.push(...);
}
| improve this answer | |

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.