From 824bf7c8794b317e1a45e8f8787871194a165fcb Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Sat, 19 Nov 2022 17:03:36 +0100 Subject: add err library --- err.svnwiki | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 err.svnwiki (limited to 'err.svnwiki') diff --git a/err.svnwiki b/err.svnwiki new file mode 100644 index 0000000..9325184 --- /dev/null +++ b/err.svnwiki @@ -0,0 +1,91 @@ += Err, a scheme library for reasoning with results + +{{err}} provides a disjoint error data type, and helper functions to +conveniently work with them. + + +== Design + +{{err}} distinguishes two types of values: +{{err}} objects, for which {{err?}} returns true, +and all others (so called {{ok}} objects). + +This means that existing code can be incorporated very easily as ok +values do not need to be wrapped. +Likewise, passing errors to existing code will trigger type errors quickly. +(Note that the empty list, the false value, and the unspecified +values are all considered {{ok}}.) + +For integrating with code which uses exceptions for error handling, +{{err}} provides the {{guard-err}} macro and the {{ok}} procedure to convert +between the two mechanisms. + +If you prefer explicit container types, you may like +SRFI 189 ("Maybe and Either"). + +If you need to deal with {{err}} values in a transparent way, +you can use SRFI 111 ("Boxes") to contain them as {{ok}} values. + + +== Core functions + +(err ) + +Returns an err object that contains {{}}. +If {{}} already is an err object, just returns {{}}. + +(unerr ) + +Returns the object wrapped in the {{err}} object {{}}. +Returns an unspecified value if {{}} is not an {{err}} object. + +(err? ) + +Returns true if {{}} is an {{err}} object, false otherwise. + +(ok? ) + +Returns true if {{}} is not an {{err}} object, false otherwise. + + +== Helpers + +(ok/if []) + +If {{}} is not an {{err}} object, calls {{}} with {{}} +as argument. If {{}} is an {{err}} object, and {{}} +is given, calls {{}} with the value that was wrapped in {{}}; +if {{}} is not given, returns {{}} as is. + +(ok=> ...) + +Successively applies functions {{}} to the value {{}} +(and then its return value etc.) as long as {{}} is {{ok?}}. + +(err=> ...) + +If {{}} is an {{err}} object, unwrap it and successively +apply the functions {{}}, else just return {{}}. +(NB: this is not the dual to {{ok=>}}, as immediate values +can be {{ok}} objects but function application continues.) + +(ok/or ...) + +Evaluate the expressions {{...}} from left to right, +stop and return as soon one is {{ok}}. + +(ok/and ...) + +Evaluate the expressions {{...}} from left to right, +stop and return as soon one is not {{ok}}. + +(guard-err [(...)] ) + +Evaluate {{}} and when an exception is raised which is listed in +{{...}} (or, by default, any exception), return the condition +object wrapped as an {{err}} object. + +(ok ) + +If {{>}} is an {{err}} object, raise it as an error (or re-raise +if it wrapped a condition). Else, return {{}}. -- cgit 1.4.1