Add Option.force_msg

This commit is contained in:
radrow 2021-07-15 20:41:38 +02:00
parent 4957d01e9e
commit dcef89b486
2 changed files with 17 additions and 1 deletions

View File

@ -1293,7 +1293,17 @@ Escapes `option` wrapping by providing default value for `None`.
Option.force(o : option('a)) : 'a
```
Forcefully escapes `option` wrapping assuming it is `Some`. Throws error on `None`.
Forcefully escapes the `option` wrapping assuming it is `Some`.
Aborts on `None`.
#### force_msg
```
Option.force_msg(o : option('a), err : string) : 'a
```
Forcefully escapes the `option` wrapping assuming it is `Some`.
Aborts with `err` error message on `None`.
#### contains

View File

@ -26,6 +26,12 @@ namespace Option =
None => abort("Forced None value")
Some(x) => x
/** Assume it is `Some` with custom error message
*/
function force_msg(o : option('a), err : string) : 'a = switch(o)
None => abort(err)
Some(x) => x
function contains(e : 'a, o : option('a)) = o == Some(e)
function on_elem(o : option('a), f : 'a => unit) : unit = match((), f, o)