1

I've got this snippet of code within a contract:

struct Brick {
    uint256 length;
    uint256 width;
    uint256 height;
}

struct Garage {
    uint256 length;
    uint256 width;
    uint256 height;
}

struct House {
    Brick[] bricks;
    Garage garage;
    bytes32 street;
    bytes32 city;
}

mapping(uint256 => House) public houses;

Without setting pragma experimental ABIEncoderV2, I get the following error when I'm trying to read any value from the houses mapping:

error: Failed to decode output: Error: insufficient data for bytes32 type (arg="", coderType="bytes32", value="0x")

However, if I make the Garage struct an array, everything's fine:

0: bytes32: street 0x0000000000000000000000000000000000000000000000000000000000000000
1: bytes32: city 0x0000000000000000000000000000000000000000000000000000000000000000

This is not a question about the ABIEncoderV2. I know that when that comes, we'll have much more flexibility as structs are concerned.

Why does it work when the property is an array and not when it's simply an instance?

1

Why does it work when the property is an array and not when it's simply an instance?

It works, because dynamic arrays are handled very similar to mappings. The length is stored at the position of the field, but index is handled like a mapping. That's also the reason you will not see it as part of your return structure (although I was expecting to see at least the length returned).

For details see https://solidity.readthedocs.io/en/develop/miscellaneous.html#layout-of-state-variables-in-storage

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.