[Ethereum] This type is only supported in the new experimental ABI encoder – why

abicontract-designcontract-developmentsoliditystruct

I have the following struct and the contract stores an array of it:

struct Issuer {
    address id;
    bytes32 name;
    IssuerStatus status;
    uint index; 
}

I also have a function that returns Issuer by its index in the array:

function getIssuerAtIndex(uint index) public view returns (Issuer) {
    return issuers[issuersIndex[index]];   
}

I use solidity 0.4.21. VS Code highlights red Issuer as the return type in the function and displays the error:

TypeError: This type is only supported in the new experimental ABI encoder. Use "pragma experimental ABIEncoderV2;" to enable the feature.
function getIssuerAtIndex(uint index) public view returns (Issuer) {
                                                          ^-------^

I can't figure out why is that type not supported as return type. Any ideas?

Best Answer

The standard ABI doesn't support structs. You can return the members of a struct in a fixed-length normally deliminated interface. For example:

function getter(bytes32 key) public returns(byte32 name, uint amount, bool isValid) :

return(myStruct[key].name, myStruct[key].amount, myStruct[key].isValid);

The idea of returning the struct object is experimental, e.g.

return(myStruct[key]);

Also, your snippet doesn't include the whole thing, but I suspect the issuerIndex is a list of keys, not the actual object. Your interface says it returns the object (the experimental feature) but possibly what you meant was returns(bytes32 key) which would be okay.

Hope it helps.

UPDATE

Based on comment below, the issue is the return value is cast as type "Issuer" which would be the experimental struct object. It should be cast as type address.

To return the address of the Issuer from the mapping:

returns(address issuer) { ...

return issuersIndex[index];

Related Topic