[Ethereum] Returning structs in new version in Solidity

remixsoliditystruct

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!

Best Answer

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
}
}
Related Topic