2022-08-19 16:25:42 +12:00
|
|
|
use Any
|
|
|
|
use Boolean
|
|
|
|
use Error
|
|
|
|
use Result
|
|
|
|
use Outrun.Core.Okay
|
|
|
|
use Outrun.Core.Error
|
2022-08-22 11:30:39 +12:00
|
|
|
use LogicalNot
|
2022-08-19 16:25:42 +12:00
|
|
|
|
2022-08-04 17:38:00 +12:00
|
|
|
```doc
|
|
|
|
# Result
|
|
|
|
|
|
|
|
The result type.
|
|
|
|
|
|
|
|
A result is anything that either succeeds, resulting in a value, or fails resulting in an error.
|
|
|
|
```
|
|
|
|
|
2022-08-07 16:20:33 +12:00
|
|
|
protocol Result, as: do
|
2022-08-04 17:38:00 +12:00
|
|
|
```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.
|
|
|
|
```
|
2022-08-22 11:30:39 +12:00
|
|
|
def error?(self: Self): Boolean, as: LogicalNot.not(okay?(self))
|
2022-08-04 17:38:00 +12:00
|
|
|
|
|
|
|
```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.
|
|
|
|
```
|
2022-09-25 14:29:37 +13:00
|
|
|
def okay(value: Any): Result, as: Outrun.Core.Okay.new(value)
|
2022-08-04 17:38:00 +12:00
|
|
|
|
|
|
|
```doc
|
|
|
|
Creates a new unsuccessful result from an error.
|
|
|
|
```
|
2022-09-25 14:29:37 +13:00
|
|
|
def error(error: Error): Result, as: Outrun.Core.Error.new(error)
|
2022-08-04 17:38:00 +12:00
|
|
|
end
|