1

Sorry if this seems dumb, I'm just getting started w/ Solidity. These are concepts I've never dealt with. I'm trying to save space by using bytes arrays, but I'm quickly getting lost. Please help.

Source

pragma solidity ^0.4.24;

contract TestConstructorAssigns {

    mapping(uint8 => bytes32[5]) questions;

    constructor() public payable {
        questions[1] = ["first part of question", "second", "third", "and so ", "on"];
    }
}

Compiler

browser/dass42-test.sol:51:24: TypeError: Type string memory[5] memory is not implicitly convertible to expected type bytes32[] storage ref.
        questions[1] = ["first part of question", "second", "third", "and so ", "on"];
                       ^------------------------------------------------------------^
1

As the compiler is telling you, you're using strings where you need bytes32s. You can fix this with an explicit cast (only needs to be on the first element):

questions[1] = [bytes32("first part of question"), "second", "third", "and so ", "on"];
  • Oh heavens, what a relief. That was like "infinite gas" less painful than I was expecting :) – tidelake Jul 30 '18 at 13:57
0

Try this:

questions[1] = [bytes32("first part of question"), bytes32("second"), bytes32("third"), bytes32("and so "), bytes32("on")];
  • 1
    @smarx's answer is correct, bc the compiler can "deduce the common type" from the explicit conversion of the first element in the array. – tidelake Jul 30 '18 at 14:03

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.