2

Why does the following not work:

pragma solidity ^0.4.24;

contract Test {

    struct S {
        string s;
        uint u;
    }

    function returnTwoValues() private returns (string s, uint u) {
        s = "123";
        u = 123;
    }

    constructor() {
        S memory s = S(returnTwoValues());
    }
}

I'm getting a

TypeError: Wrong argument count for struct constructor: 1 arguments given but expected 2.

Do I really need to store the return values in temporary variables?

  • If you're expecting to get a struct S from function returnTwoValues, then this function needs to return struct S. Otherwise, ylou should write (string s, uint u) = returnTwoValues(). – goodvibration Aug 23 '18 at 11:27
  • That's unfortunately not yet possible accross contract boundaries. I wonder mainly why, if a function returns multiple values, I can't pass them directly to a function that takes these exact values as arguments (I'm calling here the struct's constructor, which takes two arguments). – user1282931 Aug 23 '18 at 11:30
1

This works altough the entire contract is a bit weird. Why would you want to set a memory variable in the constructor? Constructor is used to initiate state variables.

pragma solidity ^0.4.24;

contract Test {

    struct S {
        string s;
        uint u;
    }

    function returnTwoValues() private returns (string s, uint u) {
        s = "123";
        u = 123;
        return(s, u);
    }

    constructor() {
        (string memory a, uint b) = returnTwoValues();
        S memory s = S(a,b);
    }
}
  • My question is why I can't do it without the temporary variables you suggest. – user1282931 Aug 23 '18 at 12:20
  • Because returnTwoValues() actually returns a tuple that consists out of (string s, uint u). A tuple would be 1 argument , therefore we need to 'deconstruct' the tuple. Previously you could have done var (a, b) = returnTwoValues(); but that's deprecated. – Nico Vergauwen Aug 23 '18 at 12:37
  • that should be an answer. Is 'Tuple' a type in solidity? can a function be written to take a tuple as argument? how so? – user1282931 Aug 23 '18 at 15:52
  • Structs are considered tuples and can be arguments and return values for internal or external-to-contract calls. – Nico Vergauwen Aug 23 '18 at 16:31
0

Try this:

pragma solidity ^0.4.24;

contract Test {

    struct S {
        string s;
        uint u;
    }

    function returnOneValue() private pure returns (S s) {
        s.s = "123";
        s.u = 123;
    }

    constructor() {
        S memory s = returnOneValue();
    }
}

Or this:

pragma solidity ^0.4.24;

contract Test {

    struct S {
        string s;
        uint u;
    }

    function returnTwoValues() private pure returns (string s, uint u) {
        s = "123";
        u = 123;
    }

    constructor() public {
        (string memory _s, uint _u) = returnTwoValues();
        S memory s = S(_s, _u);
    }
}
  • That's not what I want - such a function can't be called from an external contract and the second uses the temporary variables, which I wonder if can be avoided – user1282931 Aug 23 '18 at 11:35
  • @user1282931: You cannot return a struct outside of the contract (at present). – goodvibration Aug 23 '18 at 11:37
  • I know - that's not what I asked for. – user1282931 Aug 23 '18 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.