4

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?

  • 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. – Shawn Tabrizi Sep 13 '18 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 Sep 13 '18 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. – Shawn Tabrizi Sep 14 '18 at 1:22
  • 1
    @ShawnTabrizi, yes its exactly what I was looking for. Thank you, please post your answer. – Daniel Sep 14 '18 at 1:24
4

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.

  • 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. – Paul Razvan Berg May 6 at 23:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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