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.pk123
1 files changed, 123 insertions, 0 deletions
diff --git a/std/default/results.pk b/std/default/results.pk
new file mode 100644
index 0000000..2ac47d0
--- /dev/null
+++ b/std/default/results.pk
@@ -0,0 +1,123 @@
+## std/results
+## This module is imported by default.
+
+import std/[options, format]
+
+pub type Result[T, E] = union
+ Okay: T
+ Error: E
+
+pub type Error = interface
+ func str(self: Self)
+ func dbg(self: Self)
+
+pub type Result[T] = Result[T, ref Error]
+
+pub func is_ok[T, E](self: Result[T, E]): bool =
+ self of Okay(_)
+pub func is_err[T, E](self: Result[T, E]): bool =
+ not self.is_ok
+
+## Converts from a Result[T, E] to an Option[T].
+pub func ok[T, E](self: Result[T, E]): Option[T] =
+ if self of Okay(x):
+ Some(x)
+ else:
+ None()
+## Converts from a Result[T, E] to an Option[E].
+pub func err[T, E](self: Result[T, E]): Option[E] =
+ if self of Error(x):
+ Some(x)
+ else:
+ None()
+
+## Applies a function to T, if self is Okay.
+pub func map[T, E, U](self: Result[T, E], proc: T -> U): Result[U, E] =
+ match self
+ of Okay(x):
+ Okay(x.proc)
+ of Error(e):
+ Error(e)
+## Applies a function to E, if self is Error.
+pub func map_err[T, E, F](self: Result[T, E], proc: E -> F): Result[T, F] =
+ match self
+ of Error(e):
+ Error(e.proc)
+ of Okay(x):
+ Okay(x)
+
+## Applies a function to T, if it exists. Equivalent to .map(func).flatten.
+pub func flatmap[T, E, U](self: Result[T, E], proc: T -> Result[U, E]): Result[U, E] =
+ match self
+ of Okay(x):
+ x.proc
+ of Error(e):
+ Error(e)
+## Converts from a Result[Result[T, E], E] to a Result[T, E].
+pub func flatten[T, E](self: Result[Result[T, E], E]): Result[T, E] =
+ match self
+ of Okay(Okay(x)):
+ Okay(x)
+ of Okay(Error(e)), Error(e):
+ Error(e)
+
+## Transposes a Result[Option[T], E] to an Option[Result[T, E]].
+pub func transpose[T, E](self: Result[Option[T], E]): Option[Result[T, E]] =
+ match self
+ of Okay(Some(x)):
+ Some(Okay(x))
+ of Okay(None()), Error(_):
+ None()
+## Transposes an Option[Result[T, E]] to a Result[Option[T], E]. Takes a default error.
+pub func transpose[T, E](self: Option[Result[T, E]], error: E): Result[Option[T], E] =
+ match self
+ of Some(Okay(x)):
+ Okay(Some(x))
+ of Some(Error(e)):
+ Error(e)
+ of None():
+ Error(error)
+
+## Returns the inner value or a default.
+pub func 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 =
+ 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 =
+ match self
+ 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
+
+## Overloads the == operation for use on Results.
+pub func `==`[T, E, F](a: Result[T, E], b: Result[T, F]): bool =
+ match (a, b)
+ of (Okay(x), Okay(y)):
+ x == y
+ of _:
+ false
+
+## Overloads the str() function for use on Results.
+pub func str[T, E](self: Result[T, E]): str =
+ match self
+ of Some(x):
+ fmt("Okay({})", x.str)
+ of Error(e):
+ fmt("Error({})", e.str)
+
+examples:
+ let x: Error("fuck") = Okay(42)
+ func idk: Result[int, string]
+
+# references:
+# https://doc.rust-lang.org/std/result/enum.Result.html
+# https://github.com/arnetheduck/nim-results
+# https://github.com/codex-storage/questionable