0

I have the following structures defined in my smart contract:

mapping(bytes4 => Question) private questions;

struct Question {
    bytes8[] answers;
    mapping(bytes32 => bool) answerers;
}

Which I use to be able to create Questions, and have people answer to them "anonymously" (It's not anonymous because that's impossible on the blockchain, but at least I don't want to directly associate an answer to an answerer). The mapping in the struct Question is responsible for being easily able to access who answered already and who didn't.

However, when I try to add a new question in one of my functions, like so:

Question storage newQ;
questions[uid] = newQ;

I get the error Types in storage containing (nested) mapping cannot be assigned to. Does that mean it's impossible to use my answerers mapping in my struct? How can I go around that, and why should or shouldn't I?

1 Answer 1

1

It is not possible to assign struct with nested mappings (see v0.7.0 breaking changes). To work around this you can create an instance of your struct and then assign values to it.

Try something like this:

mapping(bytes4 => Question) private questions;

struct Question {
bytes8[] answers;
mapping(bytes32 => bool) answerers;
}

function newQuestion(bytes4 uid, bytes8 _answer, bytes32 _answerer) public {

   Question storage newQ = questions[uid];
   newQ.answers.push(_answer);
   newQ.answerers[_answerer] = true;

}

Your Answer

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

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