Does Solidity allow Struct return types?

jwgcarlylejwgcarlyle Member Posts: 29
I'm writing a function and would like to return a more complex result - all of the Solidity tutorial and examples only show returns of primitive types e.g. uint. Can a Solidity function return a struct, and if so, are there any gotchas?

Thanks,
James
http://james.carlyle.space

Best Answers

Answers

  • jwgcarlylejwgcarlyle Member Posts: 29
    Will give this a try. Many thanks. I'm using JSON-RPC so will be interested to see how it serialises the multiple return values.
  • jwgcarlylejwgcarlyle Member Posts: 29
    Update : the multiple values are simply concatenated, e.g. return 100000,100000 gives {"id":2,"jsonrpc":"2.0","result":"0x00000000000000000000000000000000000000000000000000000000000186a000000000000000000000000000000000000000000000000000000000000186a0"}
  • oomooomo Member Posts: 31
    @jwgcarlyle I was just looking at multi-valued return functionality of Solidity to address similar requirement as yours. Especially that structure as return value is not supported. I guess returning concatenated string is workable, but painful. It are also using JSON-RPC and it would be nice, if multiple return values are returned as JSON string. So for you example it would be something like

    { "a" : 1000, "b" : 100, "c" : .... }

    Currently we have a getter() defined for each field in struct
  • chrisethchriseth Member Posts: 170 ✭✭✭
    Returning structs from internal functions is now possible (externals are a bit more problematic as structs would have to be integrated into the ABI):
    function getUserRecord(address addr, uint index) internal returns (Record) {
            uint recordID = recordByUser[addr][index];
            if (recordID ==0) return;
            return records[recordID];
        }
  • oomooomo Member Posts: 31
    edited July 2015
    @chriseth This is good news. Is the copy returned or is it returning a reference? Also what is returned in case of conditional statement (recordID== 0)? Is it 0x0 or what?

    Also, I would love to see it supported for externals.
  • chrisethchriseth Member Posts: 170 ✭✭✭
    edited July 2015
    It is a bit more complicated than it looks, details should be at https://github.com/ethereum/wiki/wiki/Solidity-Tutorial#reference-types - please tell me if it does not make sense there.

    Since all variables are pre-initialised in Solidity, so are return values of struct type (with their members being initialised recursively). This means if you use
    function f() internal returns (Record r) { ... } you could also just assign the members of r individually.
    The struct itself resides in memory and the function returns a reference to this point in memory. This means that in the case of "return records[recordID]", the storage-struct is first copied to memory and then the function returns a reference to this place in memory. If you would like to return the storage reference itself, you have to use "function ... returns (Record storage) { ... }".
Sign In or Register to comment.