An Erlang library for unifying the treatment of results (values + errors)
In a language that is focused on data and messages, it makes sense for errors to be as well. This library makes it trivial for errors to be treated as such, and just as importantly, to be treated consistently across multiple problem domains (as well as different solution implementations). (When building large systems, unified/consistent results-handling (values and errors) is critical to code maintainability.)
Creating new results:
1> R1 = results:new().
{result,undefined,undefined}
2> R2 = results:new(42).
{result,42,undefined}
3> R3 = results:new(undefined, {error, oops}).
{result,undefined,{error,oops}}
4> R4 = results:new_error(oops).
{result,undefined,oops}
Checking results:
5> results:value(R2).
42
6> results:values([R1,R2,R3,R4]).
[undefined,42,undefined,undefined]
7> results:has_value(R1).
false
8> results:error(R3).
{error,oops}
9> results:errors([R1,R2,R3,R4]).
[undefined,undefined,{error,oops},oops]
10> results:has_error(R4).
true
12> results:r_and(R1, R2).
[]
13> results:r_and(R3, R4).
[{error,oops},oops]
14> results:r_or(R1, R2).
[42]
15> results:r_or(R3, R4).
[{error,oops},oops]
Using default/fallback results:
16> results:attempt(R2, results:new(5)).
{result,42,undefined}
17> results:attempt(R4, results:new(5)).
{result,5,undefined}
License ↟
Copyright © 2021, Erlang-Aided Enrichment Center
Copyright © 2018, Clojure-Aided Enrichment Center
Copyright © 2018, NASA
Distributed under the Apache License, Version 2.0.