0

I need to store some datasets as structs in mappings, that should be accessible via multiple keys. Obviously, I dont want to store the same dataset multiple times, so I want to have one copy in memory with multiple pointers pointing to it. The pointers should be stored in different mappings.

struct Dataset{
    string data;
}

mapping (bytes32 => Dataset) m1;
mapping (address => Dataset) m2;

and an insert()-function like:

function insert(bytes32 key1, address key2, string memory data) public {
    Dataset storage d = new Dataset(d);
    m1[key1] = d;
    m2[key2] = d;
    ...

Now, when I add a dataset to the contract, I'd create it, specifying it as storage, and then add it to the mapping. The idea would be that this would create only pointers instead of actual copies of the datasets. Anyhow, this throws me errors stating that storage cant be cast to memory:

TypeError: Type struct m1.Dataset memory is not implicitly convertible to expected type struct m1.Dataset storage pointer.

Any way to have a construct like this in Solidity?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.