Class: Faulty::Result
- Inherits:
-
Object
- Object
- Faulty::Result
- Defined in:
- lib/faulty/result.rb
Overview
An approximation of the Result
type from some strongly-typed languages.
F#: https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/results
Rust: https://doc.rust-lang.org/std/result/enum.Result.html
Since we can't enforce the type at compile-time, we use runtime errors to check the result for consistency as early as possible. This means we enforce runtime checks of the result type. This approach does not eliminate issues, but it does help remind the user to check the result in most cases.
This is returned from Circuit#try_run to allow error handling without needing to rescue from errors.
Constant Summary collapse
- NOTHING =
The constant used to designate that a value is empty
This is needed to differentiate between an ok
nil
value and an empty value. {}.freeze
Instance Method Summary collapse
-
#error ⇒ Object
Get the error value.
-
#error? ⇒ Boolean
Check if the value is an error value.
-
#get ⇒ Object
Get the ok value.
-
#initialize(ok: NOTHING, error: NOTHING) ⇒ Result
constructor
Create a new
Result
with either an ok or error value. -
#ok? ⇒ Boolean
Check if the value is an ok value.
-
#or_default(default = nil) { ... } ⇒ Object
Get the ok value if this result is ok, otherwise return a default.
-
#unsafe_error ⇒ Error
Get the error value without checking whether it's safe to do so.
-
#unsafe_get ⇒ Object
Get the ok value without checking whether it's safe to do so.
Constructor Details
#initialize(ok: NOTHING, error: NOTHING) ⇒ Result
Create a new Result
with either an ok or error value
Exactly one parameter must be given, and not both.
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/faulty/result.rb', line 56 def initialize(ok: NOTHING, error: NOTHING) if ok.equal?(NOTHING) && error.equal?(NOTHING) raise ArgumentError, 'Result must have an ok or error value' end if !ok.equal?(NOTHING) && !error.equal?(NOTHING) raise ArgumentError, 'Result must not have both an ok and error value' end @ok = ok @error = error @checked = false end |
Instance Method Details
#error ⇒ Object
Get the error value
108 109 110 111 |
# File 'lib/faulty/result.rb', line 108 def error validate_checked!('error') unsafe_error end |
#error? ⇒ Boolean
Check if the value is an error value
80 81 82 |
# File 'lib/faulty/result.rb', line 80 def error? !ok? end |
#get ⇒ Object
Get the ok value
89 90 91 92 |
# File 'lib/faulty/result.rb', line 89 def get validate_checked!('get') unsafe_get end |
#ok? ⇒ Boolean
Check if the value is an ok value
72 73 74 75 |
# File 'lib/faulty/result.rb', line 72 def ok? @checked = true ok_unchecked? end |
#or_default(default = nil) { ... } ⇒ Object
Get the ok value if this result is ok, otherwise return a default
128 129 130 131 132 133 134 135 136 |
# File 'lib/faulty/result.rb', line 128 def or_default(default = nil) if ok_unchecked? @ok elsif block_given? yield @error else default end end |
#unsafe_error ⇒ Error
Get the error value without checking whether it's safe to do so
117 118 119 120 121 |
# File 'lib/faulty/result.rb', line 117 def unsafe_error raise WrongResultError, 'Tried to get error for ok result' if ok_unchecked? @error end |
#unsafe_get ⇒ Object
Get the ok value without checking whether it's safe to do so
98 99 100 101 102 |
# File 'lib/faulty/result.rb', line 98 def unsafe_get raise WrongResultError, 'Tried to get value for error result' unless ok_unchecked? @ok end |