Pre-RFC: errno


#1

Summary

Use plain old integers for error handling.

Motivation

  • I’m not smart enough to understand the other error handling proposals.
  • The simplest thing we can possibly do. This has been around since the dinosaurs, and for good reason. errno has problems in C of course, but Result eliminates most of them.
  • It’s hard to imagine a newbie from another language who doesn’t already know how to use error codes. Prolog perhaps?
  • Does not require nonexistent language features, runtime support or horrible performance (allocation).

Detailed design

  1. Introduce a new data structure to the standard library, struct Errno(pub u32).
  2. Use it everywhere.
  3. Tell people to use it everywhere else.
  4. Optionally, maintain a library of globally unique aliases and related stuff.
  5. Live happily ever after.

Drawbacks

The compiler cannot constrain the set of errors each function can emit. Careless use of try! can lead to a situation much like C++ where only a recursive multi-library code audit will reveal all the exceptions thrown. RFC 70 does not solve this either.

No error chaining, if that’s a drawback.

Alternatives

RFC 70, exceptions, variadic templates, enum merging etc.

Unresolved questions

Whether there are any unresolved questions.


#2

Another drawback (or unresolved question) is passing additional information back in an error. E.g. a parse error might want to pass back the position that was incorrect, or an IO error might want to describe which file could not be opened.

Could you go into more detail about how this is an alternate error handling strategy? I’ve never seen it mentioned in this context before.


#3

The problem with Result is that it can only deal with a single error type, and we can’t decide what type that should be. With variadic anything, we could give it an arbitrary list:

Result<T, OneOf<Err1, ...>> // variadic generics
Result<T, (Option<Err1>, ...)> // variadic tuple
Result<T, OneOf(Err1, ...)> // variadic enum?

I don’t expect it would be much prettier to use than C++, but it works.