Try-Catch support uniform tuple syntax

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!

The current syntax requires you to use a temporary variable.

It works just like in C/C++ - you can omit the variable name, just keeping the type and the compiler will stop complaining that it’s not used:

try info.getInfo() returns (uint _type, uint _id, string)
2 Likes