outrun/outrun-core/lib/result.outrun

55 lines
1.2 KiB
Text

use Any
use Boolean
use Error
use Result
use Outrun.Core.Okay
use Outrun.Core.Error
use LogicalNot
```doc
# Result
The result type.
A result is anything that either succeeds, resulting in a value, or fails resulting in an error.
```
protocol Result, as: do
```doc
Returns true if the result is successful.
Must be implemented.
```
def okay?(self: Self): Boolean
```doc
Returns true if the result is an error.
```
def error?(self: Self): Boolean, as: LogicalNot.not(okay?(self))
```doc
Returns the value of the result on success.
If the result contains an `Ok` value, then the inner value will be returned.
Implementations must raise an error on failure.
```
def unwrap(self: Self): Any
```doc
Returns the error on failure.
If the result contains an `Error` value then it will be returned.
Implementations must raise an error on success.
```
def unwrap_error(self: Self): Error
```doc
Creates a new successful result from a value.
```
def okay(value: Any): Result, as: Outrun.Core.Okay.new(value)
```doc
Creates a new unsuccessful result from an error.
```
def error(error: Error): Result, as: Outrun.Core.Error.new(error)
end