DEV Community

Discussion on: More on error handling in C3

Collapse
 
powerofzero profile image
Agatha Zeren

A couple thoughts:

The implicit conditional execution seems non-ideal to me, as it seems like it could be very easily lead to bugs where one forgets to handle an error and important code is just skipped silently. If you want this pattern, (though I'm not sure that it's necessary) then perhaps use ? to indicate you are doing this. (See Swift's optional chaining):

int! foo = something_fallible();
int! bar = takes_an_int(foo?);

Also, if a function returns a void!, then is it allowed to be invoked on a line by itself?

You also, there is an interesting interaction with defer. There are cases where you want defer on normal return, error return, or both. I'm not sure what the best way to write these is. Also, it would be useful to be able to access errors in the error version of defer, for logging, etc. Or even be able to modify the error, for instance to add a stack trace. I'm not sure of the usefulness in the other cases, but they probably exist, and it would probably be good to add for symmetry. Actually, if a single defer gave access to the value, one could check whether it was an error or normal return.

Collapse
 
lerno profile image
Christoffer Lernö

There are some possibilities if one wants to guard against forgotten errors - and those are similar to Go:

  1. Require a function to store a failable in a variable, or assign it to “nothing” (_ in Go)
  2. Together with the above, require use of said variable unless it is the “nothing” variable.
  3. One can go even further requiring it is either used in a call chain or passed to either try or catch.

The above may be set as simply warnings or notices.

And yes, a function returning void! can be invoked on a line by itself.

defer, defer catch and defer try will invoke on any, error and normal return respectively.

The error is likely to be available in the catch version of the defer.

It is an interesting idea to unify them. I’ll give it some thought.