gm everyone,
I came across a not so well documented try catch block syntax use case. I want to share it so we can discuss a potential syntax change?
Context
In Info.sol
, 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 Retreive.sol
, I have a function retreiveInfo()
that uses a try catch block to call info.getInfo()
.
function retreiveInfo() public view {
try info.getInfo() returns (uint _type, uint _id, string memory _name) {
// Do something with _type and _id
// Compiler warning, unused variable
} catch {
revert();
}
}
Use Case
In that case, I only need 2 values out of the 3: _type
and _id
.
So the compiler is giving me a warning for an unused variable.
The current syntax requires you to use a temporary variable.
try info.getInfo() returns (uint _type, uint _id, string memory)
Changes?
I’m not sure if the current use of temporary variable is gas efficient or not? Tests I made locally suggest it’s neglectable.
Also, I don’t find it very intuitive considering anywhere else you would use this syntax:
(uint _type, uint _id,) = info.getInfo();
I would suggest using the above syntax for try catch as well.
Let me know what you think!