Skip to content

Commit

Permalink
add optional message to Option.getExn
Browse files Browse the repository at this point in the history
  • Loading branch information
zth committed Apr 18, 2024
1 parent 12df7e0 commit bb16b68
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- BREAKING: Use new native `bigint` type. This requires ReScript compiler version "11.1.0-rc.6" or higher. https://github.com/rescript-association/rescript-core/pull/207
- `Int`, `Float`, `BigInt`: use optional args and deprecate `xxxWithRadix`, `xxxWithPrecision` etc. https://github.com/rescript-association/rescript-core/pull/209
- Add optional `~message: string=?` to `Option.getExn`.

## 1.2.0

Expand Down
10 changes: 8 additions & 2 deletions src/Core__Option.res
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,16 @@ let forEach = (opt, f) =>
| None => ()
}

let getExn = x =>
let getExn = (x, ~message=?) =>
switch x {
| Some(x) => x
| None => raise(Not_found)
| None =>
Core__Error.panic(
switch message {
| None => "Not found."
| Some(message) => message
},
)
}

external getUnsafe: option<'a> => 'a = "%identity"
Expand Down
5 changes: 3 additions & 2 deletions src/Core__Option.resi
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,19 @@ Option.forEach(None, x => Console.log(x)) // returns ()
let forEach: (option<'a>, 'a => unit) => unit

/**
`getExn(opt)` returns `value` if `opt` is `Some(value)`, otherwise raises an exception.
`getExn(opt, ~message=?)` returns `value` if `opt` is `Some(value)`, otherwise raises an exception with the message provided, or a generic message if no message was provided.
```rescript
Option.getExn(Some(3)) // 3
Option.getExn(None) /* Raises an Error */
Option.getExn(None, ~message="was None!") /* Raises an Error with the message "was None!" */
```
## Exceptions
- Raises an error if `opt` is `None`
*/
let getExn: option<'a> => 'a
let getExn: (option<'a>, ~message: string=?) => 'a

/**
`getUnsafe(opt)` returns `value` if `opt` is `Some(value)`, otherwise `undefined`.
Expand Down

0 comments on commit bb16b68

Please sign in to comment.