outrun/outrun-core/lib/option.run

45 lines
961 B
Text
Raw Normal View History

2022-08-19 16:25:42 +12:00
use Any
use Boolean
use Option
use Outrun.Core.Some, as: Some
use Outrun.Core.None, as: None
2022-08-04 17:38:00 +12:00
```doc
# Option
The optional type.
An option is anything that an optionally provide a value. Most commonly this will be in the shape of the `Outrun.Core.Option` enum.
```
protocol Option, as: do
2022-08-04 17:38:00 +12:00
```doc
Returns true if the `Option` can provide a value, false otherwise.
Must be implemented.
```
def some?(option: Self): Boolean
```doc
Returns the value of the option, or raises an error if it can not.
Must be implemented.
```
def unwrap(option: Self): Any
```doc
Returns true if the `Option` cannot provide a value, false otherwise.
```
def none?(option: Self): Boolean, as: !some?(option)
2022-08-04 17:38:00 +12:00
```doc
Create a new "some" value using the default some type.
```
2022-08-19 16:25:42 +12:00
defs some(value: Any): Option, as: Some.new(value)
2022-08-04 17:38:00 +12:00
```doc
Create a new "none" value using the default none type.
```
2022-08-19 16:25:42 +12:00
defs none(): Option, as: None.new()
2022-08-04 17:38:00 +12:00
end