Solidity Return Struct – Creating and Using Data Structures

contract-designcontract-developmentdata-typessoliditystruct

When trying to return a struct with Solidity like this:

function getAssetStructById(uint256 assetId) constant returns (asst _asset) {
    return (assetsById[assetId]);
}

This is the compilation error it throws:

Error: Internal type is not allowed for public or external functions.

So, how can I get the data of the struct returned? I tried returning every field of the struct but this is not working anymore once you reach 9 data fields, getting the error:

'Stack too deep' compiler error.

Ref.: Is there any limitation for the number of the return values from Solidity functions?

This is the struct I would like the function to return:

struct asst {
    uint256 assetId;
    uint256 next;
    uint256 prev;
    uint256 timestampCreation;
    address assetOwner;
    address issuer;
    string content;
    uint256 sellPrice;
    assetState state;
}

Any idea how to do that on Solidity? Thx!

Ref.: Return a struct from a Solidity Contract

Best Answer

Update:

See here. We can return structs but only for internal calls.

Returning structs in new version in Solidity

In this snippet, function tryIt() returns true after a successful compile. It's just making an internal call (success). getAssetStructById() fails when called from outside.

pragma solidity 0.4.17;

contract Test {

    enum assetState{something}

    mapping(uint256 => asst) assetsById;

    struct asst {
        uint256 assetId;
        uint256 next;
        uint256 prev;
        uint256 timestampCreation;
        address assetOwner;
        address issuer;
        string content;
        uint256 sellPrice;
        assetState state;
    }

    function getAssetStructById(uint256 assetId) public view returns (asst _asset) {
        return (assetsById[assetId]);
    }

    function tryIt(uint id) public view returns(bool success) {
        asst memory a = getAssetStructById(id); 
        return true;
    }
}

Hope it helps.