Description
Before the file system layer (see #201, #220) in stdlib can be implemented, it would be important to reach some agreement on how to signalize errors within stdlib. I am not talking about logging, as this is an add on we can discuss later. I want to concentrate here on how errors can be passed between routines in stdlib.
Fortran's current approach to report status in I/O commands is to have some optional integer argument, returning the status of the operation. If the argument has not been specified, the code stops in case of errors. I'd propose to generalize this principle, but using derived types to achieve higher robustness and flexibility:
- The status should become a derived type (
type(status)
), which methods allowing to set and query it. - The status-type has a finalizer. If it goes out of scope with an unhandled error in it (without having been handled by the caller), it calls
error stop
. - The derived has a special field which contains a
class(error)
item. It can carry arbitrary types derived from a base class, so that arbitrary complex error information can be passed around. This way, the error signaling mechanism is extensible, we could even think to add an error-hierarchy as we have in Python.
I have made a toy project to demonstrate the principle. Please, have a look at it and let me know what you think.
A few more notes:
- This error passing should be applied to cases, where error reporting via special values in not feasible (e.g.
change_dir()
). - We could of course implement in the error-class to information about how it was propagate (backtrace).
This issue is related to several other error discussons, e.g. #219 , #193, #95. My suggestion is, again, to first reach agreement on the low-level of the error reporting, and we can add extended functionality, such as logging, on top later.