1

I'm getting this error

TypeError: The struct has all its members omitted, therefore the getter cannot return any values. --> Test.sol:11:5

for the following smart contract code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

struct A
{
    uint[] values;
}

contract Test
{
    A public a;
}

The error is no longer issued if I add at least 1 non-array member of either a primitive or a custom (user-defined) type to the struct A.

Why does this happen?

1 Answer 1

2

See the docs

  • When set A public a, it auto generate a defaut getter for a. And follow the docs, nothing is return in that default getter, so it raised the issue. But when you put 1 non-array member in struct, it's possible. This is example
struct A
{
    uint[] values;
}

contract Test
{
    A public a;

    // default getter
    function a() public view returns() {}; // error
}

struct A
{
    uint[] values;
    uint256 value2;
}

contract Test
{
    A public a;

    // default getter
    function a() public view returns(uint256 value2) {
       value2 = a.value2;
    }; // success
}
  • You could try this, let's remove public & declare your own getter.
struct A
{
    uint256[] values;
}

contract TestA
{
    A a;

    function set(uint256 _b) external {
        a.values.push(_b);
    }
    function get() external view returns(A memory) {
        return a;
    }
}
3
  • thank you for the reference to the documentation!
    – Iaroslav
    Mar 9, 2023 at 16:19
  • What I understood from it, is that the part of Solidity that is responsible for automatically generating getters for the public state members of a contract, for some reason, intentionally "omits" mappings and arrays (with the exception of byte arrays). What I didn't understand, is why it happens like that if it's possible to return a struct containing an array explicitly (what you did in your last code sample)... Any explanation for the "why"? :)
    – Iaroslav
    Mar 9, 2023 at 16:35
  • In my opinion: - mapping: can't know which is the key to retrieve value. - array: it's a default getter, it's a view function. But view functions doesn't mean it's free gas, it's just a simulation of your execution off-chain without sending a tx. Solidity doesn't have hard limit for response data but it has a limitation for gas, So I think it doesn't want your default getter "dead" unexpected => it doesn't auto return array by default getter. As you know, a default getter of an array is: get(uint index), not full of array. So basically => same with mapping because no key to retrieve value
    – CT95
    Dec 27, 2023 at 3:14

Your Answer

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

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