7

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?

5

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];

  • issuerIndex is an index of addresses; issuers is a map of addresses to Issuer objects. Thus, to get an Issuer by index, I first get Issuer's address from issuerIndex and then the actual Issuer from the issuers map. I do it because sometimes I need to get Issuer by index and sometimes by addres, so i have to maintain both. Thank you for your explanation! – Andrey Mar 17 '18 at 19:10
  • Update ^ for clarity. – Rob Hitchens - B9lab Mar 17 '18 at 19:58

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.