Skip to content

Commit bb16b68

Browse files
committed
add optional message to Option.getExn
1 parent 12df7e0 commit bb16b68

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- 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
66
- `Int`, `Float`, `BigInt`: use optional args and deprecate `xxxWithRadix`, `xxxWithPrecision` etc. https://github.com/rescript-association/rescript-core/pull/209
7+
- Add optional `~message: string=?` to `Option.getExn`.
78

89
## 1.2.0
910

src/Core__Option.res

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,16 @@ let forEach = (opt, f) =>
3434
| None => ()
3535
}
3636

37-
let getExn = x =>
37+
let getExn = (x, ~message=?) =>
3838
switch x {
3939
| Some(x) => x
40-
| None => raise(Not_found)
40+
| None =>
41+
Core__Error.panic(
42+
switch message {
43+
| None => "Not found."
44+
| Some(message) => message
45+
},
46+
)
4147
}
4248

4349
external getUnsafe: option<'a> => 'a = "%identity"

src/Core__Option.resi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,19 @@ Option.forEach(None, x => Console.log(x)) // returns ()
6666
let forEach: (option<'a>, 'a => unit) => unit
6767

6868
/**
69-
`getExn(opt)` returns `value` if `opt` is `Some(value)`, otherwise raises an exception.
69+
`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.
7070

7171
```rescript
7272
Option.getExn(Some(3)) // 3
7373
Option.getExn(None) /* Raises an Error */
74+
Option.getExn(None, ~message="was None!") /* Raises an Error with the message "was None!" */
7475
```
7576

7677
## Exceptions
7778

7879
- Raises an error if `opt` is `None`
7980
*/
80-
let getExn: option<'a> => 'a
81+
let getExn: (option<'a>, ~message: string=?) => 'a
8182

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

0 commit comments

Comments
 (0)