aboutsummaryrefslogtreecommitdiff
path: root/std/default/results.pk
diff options
context:
space:
mode:
Diffstat (limited to 'std/default/results.pk')
-rw-r--r--std/default/results.pk27
1 files changed, 14 insertions, 13 deletions
diff --git a/std/default/results.pk b/std/default/results.pk
index 8faf0c0..8e21234 100644
--- a/std/default/results.pk
+++ b/std/default/results.pk
@@ -1,13 +1,12 @@
## std/results: Result types.
## This module is imported by default.
-import std/[options, format]
+use std[options, format]
-## The Result type.
-## A type that represents either success or failure.
+## The Result type. Represents either success or failure.
pub type Result[T, E] = union
- Okay: T
- Error: E
+ Okay(T)
+ Error(E)
## The Err interface. Useful for dynamically dispatching errors.
pub type Err = interface
@@ -52,7 +51,7 @@ pub func map[T, E, U](self: Result[T, E], fn: T -> U): Result[U, E] =
pub func map_err[T, E, F](self: Result[T, E], fn: E -> F): Result[T, F] =
match self
of Error(e):
- Error(e.fn)
+ Error(fn(e))
of Okay(x):
Okay(x)
@@ -92,25 +91,27 @@ pub func transpose[T, E](self: Option[Result[T, E]], error: E): Result[Option[T]
pub func get_or[T, E](self: Result[T, E], default: T): T =
if self of Okay(x): x
else: default
+
## Directly accesses the inner value. Throws an exception if `Error`.
-pub yeet func `!`[T, E](self: Result[T, E]): T =
+pub func ![T, E](self: Result[T, E]): T =
match self
of Okay(x): x
- of Error(e): raise Exception(e) # todo: syntax??
-pub yeet func get_err[T, E](self: Result[T, E]): E =
+ of Error(e): raise e
+## Directly accesses the inner error. Throws an exception of type T if `Okay`.
+pub func get_err[T, E](self: Result[T, E]): E =
match self
of Error(e): e
- of Okay(x): raise Exception(x) # todo: syntax??
+ of Okay(x): raise x
## Indirect access. Propagates `Error`.
-macro `?`[T, E](self: Result[T, E]) =
+pub macro ?[T, E](self: Result[T, E]) =
quote:
- match self
+ match `self`
of Okay(x): x
of Error(e): return Error(e)
## Overloads the `==` operation for use on Results.
-pub func `==`[T, E, F](a: Result[T, E], b: Result[T, F]): bool =
+pub func ==[T, E, F](a: Result[T, E], b: Result[T, F]): bool =
match (a, b)
of (Okay(x), Okay(y)):
x == y