outrun/lib/result.run

48 lines
1.1 KiB
Text
Raw Normal View History

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.
```
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.
```
def error?(self: Self): Boolean, as: !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.
```
defs okay(value: Any): Result, as: Outrun.Core.Okay.new(value)
```doc
Creates a new unsuccessful result from an error.
```
defs error(error: Error): Result, as: Outrun.Core.Error.new(error)
end