result
Source: /nix/store/mls72plk3raskl1r5afh3cl9ik3rn969-source/nix/result.nix
Module Description
Simple Result-based error handling built on condition system.
Provides familiar throw/catch semantics as a convenience layer over the more powerful condition system. Unlike conditions, Results always unwind the stack on error.
Comparison
| Result | Conditions |
|---|---|
| throw’/catch’ | signal/handle |
| Always unwinds | Can resume |
| Simple | Powerful |
| One handler wins | Multiple handlers |
Example
result = catch' (
divide = x: y:
if y == 0
then throw' "DivideByZero" { dividend = x; }
else pure (x / y);
divide 10 2
);
# => Ok 5
result2 = catch' (divide 10 0);
# => Err { type = "DivideByZero"; dividend = 10; }
When to Use
- Use Result for simple error propagation
- Use Conditions when you need resumption or multiple handlers
Combinators
bindResult
Chain Result-producing operations.
If the Result is successful, applies the function to the value. Otherwise passes the error through.
Type Signature
(A -> Result<B>) -> Result<A> -> Result<B>
Parameters
f: Function returning a Resultresult: Input Result
Example
validateAge = age:
if age < 0 then { success = false; error = "negative"; }
else { success = true; value = age; };
bindResult validateAge { success = true; value = 25; }
# => { success = true; value = 25; }
catch'
Catch errors thrown with throw’.
Wraps an effect to catch any thrown errors and return them as Result values: { success = true; value = v; } for success or { success = false; error = e; } for errors.
Type Signature
Fx<S, V> -> Fx<S, Result<V>>
Parameters
effect: Effect that may throw errors
Returns
Result with either:
{ success = true; value = V; }{ success = false; error = ErrorDetails; }
Example
result = catch' (
bind (pure 10) (x:
if x > 5
then throw' "TooBig" { value = x; }
else pure x
)
);
# => { success = false; error = { type = "TooBig"; value = 10; }; }
See Also
throw'- Throw an errortry- Catch with default value
mapResult
Transform the value inside a successful Result.
Applies a function to the value if the Result is successful, otherwise passes the error through unchanged.
Type Signature
(A -> B) -> Result<A> -> Result<B>
Parameters
f: Function to apply to successful valueresult: Result to transform
Example
mapResult (x: x * 2) { success = true; value = 21; }
# => { success = true; value = 42; }
mapResult (x: x * 2) { success = false; error = {...}; }
# => { success = false; error = {...}; }
throw'
Throw an error that unwinds the stack.
Signals an error condition that cannot be resumed. The error will propagate up to the nearest catch’ handler.
Type Signature
String -> Attrs -> Fx<S, never>
Parameters
errorType: Error type identifierdetails: Additional error information
Example
validateAge = age:
if age < 0
then throw' "InvalidAge" { age = age; }
else pure age;
See Also
catch'- Handle thrown errorstry- Catch with default valueerror- Non-resumable condition system error
try
Try an effect, returning a default value on error.
Convenience wrapper around catch’ that extracts the value or returns the default on error.
Type Signature
V -> Fx<S, V> -> Fx<S, V>
Parameters
default: Value to return if effect throwseffect: Effect to try
Example
result = try 0 (
if somethingWrong
then throw' "Failed" {}
else pure 42
);
# => 42 on success, 0 on error
See Also
catch'- Catch and inspect errorthrow'- Throw an error
Generated from /nix/store/mls72plk3raskl1r5afh3cl9ik3rn969-source/nix/result.nix