0

I get an error:

error[9582]: TypeError: Member "length" not found or not visible after argument-dependent lookup in function (uint256) view external returns (uint256,uint256,uint256,bytes32).

This is my code:

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

import "forge-std/Test.sol";

contract StructDef {
    struct Edition {
        uint256 startIndex;
        uint256 endIndex;
        uint256 minted; // store how many have been minted in the edition
        bytes32 merkleProof;
    }

    Edition[] public editions;

    function addEdition(
        uint256 startIndex,
        uint256 endIndex,
        bytes32 merkleProof
    ) public {
        editions.push(
            Edition({
                startIndex: startIndex,
                endIndex: endIndex,
                minted: 0,
                merkleProof: merkleProof
            })
        );
    }
}

contract StructDefTest {
    StructDef public structDef;

    function setUp() public {
        structDef = new StructDef();
    }

    function testEditions() public {
        structDef.addEdition(1, 10000, 0x0);
        structDef.addEdition(10001, 99999, 0x0);
        assert(structDef.editions.length == 2); //error[9582]: TypeError: Member "length" not found or not visible after argument-dependent lookup in function (uint256) view external returns (uint256,uint256,uint256,bytes32).
    }
}

As you an see I'm using Foundry, but that is not the cause of the error I'm sure.

How would I test the number of items in the editions array?

1
  • Try structDef.editions().length =….
    – Majd TL
    May 24, 2022 at 22:05

1 Answer 1

1

from the doc:

If you have a public state variable of array type, then you can only retrieve single elements of the array via the generated getter function. This mechanism exists to avoid high gas costs when returning an entire array. You can use arguments to specify which individual element to return, for example data(0). If you want to return an entire array in one call, then you need to write a function, for example:

function getArray() public view returns (uint[] memory) {
    return myArray;
}

here you have an example of the generated getter

getter function generated by the compiler

as you can see the getter function doesn't return the entire array and that is the reason why you can't access to the array length

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.