diff options
Diffstat (limited to 'std')
-rw-r--r-- | std/default/format.pk | 4 | ||||
-rw-r--r-- | std/default/options.pk | 6 | ||||
-rw-r--r-- | std/default/results.pk | 16 |
3 files changed, 20 insertions, 6 deletions
diff --git a/std/default/format.pk b/std/default/format.pk index ec573ef..ee8997d 100644 --- a/std/default/format.pk +++ b/std/default/format.pk @@ -7,13 +7,13 @@ pub type Display = interface str(Self): str dbg(Self): str -## The Debug interface. Broadly implemented for every type with magic, +## The Debug interface. Broadly implemented for every type with compiler magic, ## types can (and should) override the generic implementations. pub type Debug = interface dbg(Self): str ## Prints all of its arguments to the command line. -pub proc print(params: varargs[Display]) = +pub func print(params: varargs[Display]) = stdout.write(params.map(x => x.str).join(" "), "\n") ## Prints all of its arguments to the command line, in Debug form. diff --git a/std/default/options.pk b/std/default/options.pk index bbe0d9c..397a8f3 100644 --- a/std/default/options.pk +++ b/std/default/options.pk @@ -3,14 +3,18 @@ import std/format +## The `Option` type. +## A type that represents either the presence or absence of a value. pub type Option[T] = union Some: T None +## Checks if a type is present within an `Option` type. pub func is_some[T](self: Option[T]): bool = self of Some(_) +## Checks if a type is not present within an `Option` type. pub func is_none[T](self: Option[T]): bool = - not self.is_some + self of None ## Converts an `Option[T]` to a `Result[T, E]` given a user-provided error. pub func err[T, E](self: Option[T], error: E): Result[T, E] = diff --git a/std/default/results.pk b/std/default/results.pk index 4e4d27a..8faf0c0 100644 --- a/std/default/results.pk +++ b/std/default/results.pk @@ -3,20 +3,30 @@ import std/[options, format] +## The Result type. +## A type that represents either success or failure. pub type Result[T, E] = union Okay: T Error: E -pub type Error = interface +## The Err interface. Useful for dynamically dispatching errors. +pub type Err = interface str(Self): str dbg(Self): str -pub type Result[T] = Result[T, ref Error] +## A `Result` type that uses dynamically dispatched errors. +## The `Error` may be any type implementing `Err`. +pub type Result[T] = Result[T, ref Err] +## A `Result` type that only checks for success. +## Does not contain a value. +pub type Success[E] = Result[Unit, E] +## Checks if a `Result` type was successful. pub func is_ok[T, E](self: Result[T, E]): bool = self of Okay(_) +## Checks if a `Result` type was not successful. pub func is_err[T, E](self: Result[T, E]): bool = - not self.is_ok + self of Error(_) ## Converts from a `Result[T, E]` to an `Option[T]`. pub func ok[T, E](self: Result[T, E]): Option[T] = |