Why are state variables wrapped in a struct?

Hi there, the Counter library has _value wrapped in the struct Counter. I’m wondering if this is for hiding the value from being read directly in the storage slot or there is any other reasons. Thanks.

    struct Counter {
        // This variable should never be directly accessed by users of the library: interactions must be restricted to
        // the library's function. As of Solidity v0.5.2, this cannot be enforced, though there is a proposal to add
        // this feature: see https://github.com/ethereum/solidity/issues/4637
        uint256 _value; // default: 0
    }

And I did two tests given below, it seems that whether using a struct or not does not change the storage layout patter at all.

contract Test {

    bytes32 _sealedSeed;
    uint128 _val1;
    uint128 _val2;
    bool _flag1;
    bool _flag2;
    uint256 _val3;
}

Contract Test2 {
    Struct Val {
        bytes32 _sealedSeed;
        uint128 _val1;
        uint128 _val2;
        bool _flag1;
        bool _flag2;
        uint256 _val3;
    }
    Val val;
}
1 Like