1

Disclaimer

I came across an not so well documented issue so I decided to share it and show the solution I found.

This is a very simple problem with a very simple solution, I just didn't found any other topic mentionning it.

I hope this can help someone, someday. If you have a better approach for this let me know!


Context

In ContractA, I have a function getInfo() that returns 3 parameters :

function getInfo() public pure returns (uint type, uint id, string memory name) {
  return (0, 1, "info");
}

In ContractB, I have a function retreiveInfo() that uses a try catch block to call ContractA.getInfo().

function retreiveInfo() public view {
  try ContractA.getInfo() returns (uint _type, uint _id, string memory _name) {
    // Do something with _type and _id
    // Compiler warning, unused variable
  } catch {}
}

Problem

The problem is that I only need 2 values out of the 3: _type and _id. So the compiler is giving me a warning for an unused variable.

I tried to use this syntax:

try ContractA.getInfo() returns (uint _type, uint _id, )

But compiler returns error "Unexpected trailing coma in parameter list".

So I tried not to include it at all:

try ContractA.getInfo() returns (uint _type, uint _id)

Again, compiler error "Function returns 3 values, but returns clause has 2 variables".


Solution

The solution I found is to use an temporary variable:

try ContractA.getInfo() returns (uint _type, uint _id, string memory)
2
  • It is a quite common trick for C like languages. You could write the last part as an answer so the question can be mark as resolved.
    – Ismael
    Commented Feb 26 at 19:34
  • That's right, I learned about this by doing more research but I still haven't seen this mentioned anywhere for Solidity, thanks!
    – Saucy
    Commented Feb 27 at 20:18

1 Answer 1

1

Solution

The solution I found is to use an temporary variable:

try ContractA.getInfo() returns (uint _type, uint _id, string memory)
1
  • Add explanation for the rationale behind using a temporary variable.
    – DevCrypto
    Commented Mar 5 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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