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)