0

I have a function which returns multiple values.

In some places I use all of them, while in other places I use only some of them.

This short snippet can illustrate the problem:

contract A {
    uint256 x;
    uint256 y;
    uint256 z;
    uint256 w;
    function getVars() external constant returns (uint256, uint256, uint256, uint256) {
        return (x, y, z, w);
    }
}

contract B {
    A private a;

    function func1() external {
        var (x, y, z, w) = a.getVars();
        // Do something with all variables (x, y, z, w)
    }

    function func2() external {
        var (x, y, z, w) = a.getVars();
        // Do something with only some variables (x, y)
    }
}

For which, Solidity compiler gives me the following two warnings:

contracts/B.sol: Warning: Unused local variable.
        var (x, y, z, w) = a.getVars();
                   ^
contracts/B.sol: Warning: Unused local variable.
        var (x, y, z, w) = a.getVars();
                      ^

How would I best resolve this problem?

Of course, I could just "state" them explicitly, for example:

    function func2() external {
        var (x, y, z, w) = a.getVars();
        (w, z); // Avoid compilation warning
        // Do something with only some variables (x, y)
    }

But it seems like a pretty ugly solution.

Is there a compilation flag which I can apply only for this specific file?

Or ideally, is there something "nicer" that I could put in the code in order to revoke this warning?

Thank you.

1

Just delete the variable that you are declaring, but don't use e.g.

var (x, y,, w) = a.getVars();
  • With the var keyword deprecated in Solidity 0.4.21 and onward, after replacing it with uint256, your solution no longer works. Any suggestions? Thanks – goodvibration Jun 11 '18 at 7:37
1

Now that 'var' is deprecated, it looks like you can simply drop the var keyword and add the type within the brackets -

(uint256 x, uint256 y,, uint256 w) = a.getVars();   

seems to work for me...

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.