1

Here is my code example:

pragma solidity ^0.5.16;
pragma experimental ABIEncoderV2;

contract Operations {

    Operation[] public operations;

    struct Operation {
        uint startOperation;
        uint endOperation;
        string responsible; 
        string opType;
    }

    function getAllOperations() public view returns(Operation[] memory){
        return operations;
    }
}

I have 3 questions:

1 - Why do I need pragma experimentals ABI to return an Array of structs?

2 - Why do I have a warning saying I shouldn't use experimental features in live deployments?

3 - Is there any other way of doing this without using an experimental feature?

2

1 - Check this for more detailed info

2 - If you switch the pragma solidity 0.5.X to 0.6.2 (last one), you will see that you are able to deploy the same contract with no warning saying "you shouldn't deploy the experimental features in live networks".

3- Not for now, if you use solc bellow 0.6.X, the only way of retrieving the values by key/index

Hope this helps

1

Yes - use Solidity 0.6.x where the ABIEncoderV2 is no longer considered experimental ;). They've been working on it for a while, and it's been available to test in earlier versions during this period.

Having said that, you still need to use a pragma to enable it in 0.6, but hopefully it all works now!

0

Just an update - the v2 encoder is the default encoder from Solidity 0.8 and up (see docs), meaning that you have the ability to pass in structs by default.

To summarize the details in the other answers here (and this answer) and docs, from 0.6 and up the v2 encoder was no longer considered experimental, and could be invoked using:

pragma abicoderv2;

For Solidity before 0.6, you'd need:

pragma experimental ABIEncoderV2;

Let's get back to your first question, though. Why is did it take an experimental encoder to do this (return an array of structs) for so long?

I haven't found a direct answer to this, but after reading this excellent article on dealing with v1 encoder structs, it would seem the answer is this:

The v1 encoder seems to have generally broken down structs into tuples. Tuples are not an exposed type in Solidity - you can't make them directly, the compiler does it under the hood sometimes. Once the data from the structs was generally in tuple-land, it meant that as an internal data type, it was subject to a number of limitations.

This is guesswork, though. If anyone else has feedback or a more accurate answer, feel free to point us in the right direction!

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.