[Ethereum] TypeError: This type is only supported in the new experimental ABI encoder

remixsolidity

I am practicing my Solidity skills and while I did expect to get an error, I did not expect this error

TypeError: This type is only supported in the new experimental ABI
encoder. Use "pragma experimental ABIEncoderV2;" to enable the
feature. function getArray() public view returns (string[]) {

This is my code in Remix:

pragma solidity ^0.4.17;

contract Test {
    string[] public myArray;

    function Test() public {
        myArray.push("hola");
    }

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

What gives here?

Is this because the standard ABI does not support dynamic nested arrays?

Best Answer

The error message you are seeing is a result of the fact that Solidity does not yet support two levels of dynamic arrays, which a string[] would be:

Is it possible to return an array of strings (string[]) from a Solidity function?

Not yet, as this requires two levels of dynamic arrays (string is a dynamic array itself).

The error is telling you to add an additional line to the top of your code:

pragma experimental ABIEncoderV2;

This feature came in Solidity version 0.4.19, which states:

Code Generator: New ABI decoder which supports structs and arbitrarily nested arrays and checks input size (activate using pragma experimental ABIEncoderV2;).

Note that in this case it does support nested arrays. I assume these features will eventually make their way into the production builds of Solidity, but it is not clear to me when. I tested, and your code does work when I add this additional pragma in remix.

Related Topic