7

I have been reading about Solidity release version 0.4.17. https://github.com/ethereum/solidity/releases

One of the following statements was really interesting:

We also worked further on the new ABI encoder: Functions can now return structs. Switch it on using pragma experimental ABIEncoderV2. It should already work, but still generates more expensive code.

I`ve been trying to simulate that in Remix but without any luck, I am getting this error:

"error": "Failed to decode output: Error: Unsupported or invalid type: tuple" Here is my code:

pragma solidity ^0.4.17;
pragma experimental ABIEncoderV2;
contract StructTest{
    struct someTestStruct {
        uint A;
        uint B;
        bytes32 C;
    }
    someTestStruct str;
    function StructTest(){
        str = someTestStruct({A: 5, B: 6, C: "Hi"});
    }
    // trying to get struct but getting "error": "Failed to decode output: Error: Unsupported or invalid type: tuple"
    function returnStruct() returns (someTestStruct){
        return str;
    }
}

Have someone tried this? What am I doing wrong here? Thanks in advance!

4

Yes we can return structs.

But only in internal function calls.

pragma solidity ^0.4.19;

contract tester{

struct Person
{
    string name;
    uint age;
}

function getSome() public returns (Person a)
{
    Person memory p;
    p.name = "kashish";
    p.age =20;
    return p;
}

function wantSome() public returns (string,uint)
{
     Person memory p2 =getSome();
     return (p2.name,p2.age); // return multiple values like this
}
}
0

Might worth checking the Solidity compiler version in Remix's "Settings" menu. The code appears to be running OK in my test. So it could be the compiler isn't the latest.

  • 1
    Changing compiler version didn`t helped. Do you getting struct in "decoded output".? – Sivich Oct 30 '17 at 8:51
  • Looks like my answer is wrong. I also got decode error when the details are expanded. It seems to not many ABIs support this yet. See github.com/ethereum/solidity/issues/2948 . So it seems at least it won't work with Remix (at least for now) – Linmao Song Oct 30 '17 at 23:32
  • Ok, thanks for your answer! I`ll try it with Truffle or Meteor then. – Sivich Oct 31 '17 at 10:49
0

With the new solidity compiler this is possible. Probably you would have to end up adding the pragma declaration for experimental ABIEncoderV2 at the top of the contract definition file. I am using 0.5.9 and works fine.

pragma solidity ^0.5.9;
pragma experimental ABIEncoderV2;

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.