7

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?

4
  • Are you sure you didn't cut off part of the error message? This is what I get: TypeError: This type is only supported in the new experimental ABI encoder. Use "pragma experimental ABIEncoderV2;" to enable the feature. Commented Sep 13, 2018 at 22:49
  • 1
    @ShawnTabrizi, my apologies, I did not realize the error was that long until I extended it to take up almost the whole browser. I edited my post to reflect this.
    – Daniel
    Commented Sep 13, 2018 at 23:42
  • 1
    Yeah, in this case, the error message is exactly what I would expect. string[] is not yet supported as a return value for a solidity function, but is planned to be supported sometime in the future, and they have started that implementation using the "experimental ABI Encoder V2". If this is a qualified answer, let me know and I will respond with similar details. Commented Sep 14, 2018 at 1:22
  • 1
    @ShawnTabrizi, yes its exactly what I was looking for. Thank you, please post your answer.
    – Daniel
    Commented Sep 14, 2018 at 1:24

2 Answers 2

12

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.

1
  • 1
    Just a word of caution: on the 26th of March, the Solidity team disclosed a (small) vulnerability found in Solidity 0.5.6 and below when using pragma experimental ABIEncoderV2. Commented May 6, 2019 at 23:33
2

Or just use

 pragma solidity ^0.8.0;

From Solidity v0.8.0 Breaking Changes https://docs.soliditylang.org/en/v0.8.0/080-breaking-changes.html

ABI coder v2 is activated by default.

So it is not experimental, but standard feature since solc v0.8.0

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.