10

On-chain example:

pragma solidity 0.4.24;

contract MyContract {
    uint[] public array;

    constructor() public {
        array.push(1);
        array.push(2);
        array.push(3);
    }

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

Off-chain example:

contract("MyContract", function(accounts) {
    it("Test", async function() {
        myContract = await artifacts.require("MyContract.sol").new();
        let array = await myContract.getArray();
        let item0 = await myContract.array(0);
    });
});

As you can see, the getArray function indeed returns the array.

But when I "call" the array, I need to pass an index (in other words, the array "returns" an item).

Attempting to execute:

let array = await myContract.array();

Results with:

Invalid number of arguments to Solidity function

Why has the Solidity standard defined it this way?

Thank you!

1
  • 2
    Surprising how hard it is to find a good answer to this question. The only thing I've found is that gas considerations played into the design. Maybe an example of (over)protecting the user... Commented Sep 2, 2019 at 17:57

2 Answers 2

7

Solidity creates getter functions automatically for public variables. As your array is public, a public getter function a generated for it automatically. The getter function is used to access the array variables directly and not for retrieving the array itself.

For array types, the getter function requires an integer parameter to denote the index to access. Read more at https://solidity.readthedocs.io/en/v0.4.24/types.html#arrays

So your JavaScript code is simply accessing its public getter function. But, as you noticed, you can also get the original array if you have a custom getter function.

2
  • 9
    Thanks. You basically described everything that I already know (and have already described myself as part of the question), while leaving the actual question unanswered - why has the language standard decided that getter functions for arrays are to return an element in the array rather than the entire array itself (as one would normally expect from a getter function)? Commented Oct 12, 2018 at 20:56
  • 1
    Ah, sorry, misread that part and misinterpreted your question's topic. Commented Oct 13, 2018 at 5:37
6

This mechanism exists to avoid high gas costs when returning an entire array.

Taken form Documentation, which is interesting to understand for me that it could be related to best practice to abstract data and also costs associated to it. There might be special cases where you might need the whole list. I guess stored for those cases would be better for store in some IPFS technology. I am not the expert but trying to collaborate and explore more on this even.

1
  • 1
    This should be marked correct imo. The quoted documentation is the precise answer to the question being asked. Commented Jul 13, 2023 at 4:58

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.