diff options
Diffstat (limited to 'std/default/results.pk')
-rw-r--r-- | std/default/results.pk | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/std/default/results.pk b/std/default/results.pk index 2ac47d0..187ece9 100644 --- a/std/default/results.pk +++ b/std/default/results.pk @@ -1,4 +1,4 @@ -## std/results +## std/results: Result types. ## This module is imported by default. import std/[options, format] @@ -7,9 +7,11 @@ pub type Result[T, E] = union Okay: T Error: E +# todo: determine the difference between interfaces and types +# ErrorInterface? Errorable? Err? pub type Error = interface - func str(self: Self) - func dbg(self: Self) + str(Self): str + dbg(Self): str pub type Result[T] = Result[T, ref Error] @@ -79,11 +81,11 @@ pub func transpose[T, E](self: Option[Result[T, E]], error: E): Result[Option[T] Error(error) ## Returns the inner value or a default. -pub func or[T, E](self: Result[T, E], default: T): 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(e). -pub yeet func get[T, E](self: Result[T, E]): T = +pub yeet func `!`[T, E](self: Result[T, E]): T = match self of Okay(x): x of Error(e): raise Exception(e) # todo: syntax?? @@ -92,10 +94,12 @@ pub yeet func get_err[T, E](self: Result[T, E]): E = of Error(e): e of Okay(x): raise Exception(x) # todo: syntax?? -# todo: direct access, alias to get -macro `!`[T, E](self: Result[T, E]): T -# todo: indirect access, propagates Err -macro `?`[T, E](self: Result[T]): T +## Indirect access. Propagates Error. +macro `?`[T, E](self: Result[T, E]) = + quote: + 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 = |