How can I return struct when function is called?

  • I'd like to return struct type User. However, when I tried the following getUser function, it returns an error. Is there any way that struct data would be returned from a function?


    struct User{
        uint256 user_id;
        bytes32 name;
        bytes32 address;
        bytes32 birth_day;
    mapping (uint256 => User) public users;
    function getUser(uint256 user_id) constant returns (User) {
            return users[user_id];


    client/lib/contracts/User.sol: Solidity errors: :125:58: Error: Expected type name
    function getUser(uint256 user_id) constant returns (struct User) {

  • You can not return a struct because Solidity implements them only as a loose bag of variables, they are not real objects.

    You can use a solution from this answer:


    Since 0.4.17 you can use pragma experimental ABIEncoderV2 to return structs. Of course, until the experimental keyword is removed, it's not safe to use it in production apps.

    The answer is a year old. Are there any updates? Or we still can not return struct array from solidity function.

    @PrashantPrabhakarSingh I updated the answer.

    @PaulBerg again one year from now ;) any update?

    @Senju It's safe to use it in Production, it's been 3 years after all

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM