[Ethereum] Return a struct from a Solidity Contract

soliditystruct

I'm just trying to get confirmation as to whether this is supported within Solidity currently. Older responses to this question seem to imply this has been added, but I can't find any concrete literature to confirm.

If I have a struct within the contract, can I return it via a getter on the contract? For example:

struct myDetails {
    string firstName;
    string lastName;
}

function getName() returns (???) {
    return myName;
}

Thx

Marty

Best Answer

It's not currently possible to return a struct to the outside with your own function. Nonetheless, it's possible with Solidity's automatically generated getters.

By marking a variable public (for example uint public someNumber) Solidity will automatically generate a getter (someContract.someNumber()). This will indeed work on structs (Foo public someFoo) and even arrays and mappings! (mapping(uint => player) public players) In the last case, you provide a key to the getter to get the one you want. (someContract.players(7))

One caveat: Nested structs, arrays, and mappings inside of structs are currently not returned by getters. Strings, however, are.

Related Topic