diff options
author | JJ | 2023-10-23 02:34:17 +0000 |
---|---|---|
committer | JJ | 2023-10-23 02:34:17 +0000 |
commit | d5f89ba0e2fd0458f4e51e13233069583d5a89ac (patch) | |
tree | 658019acfbd5cec5c5944accb95d0fde618da050 /std/default/results.pk | |
parent | 2d5d47c89d5b9df3535029f04cf1205e47276451 (diff) |
std: broad updates; add ast, iterators, format, tests, logs
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 = |