Skip to content

Commit f3cc3c3

Browse files
committed
Add Belt.Option.getOrThrow
1 parent 3a37bac commit f3cc3c3

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
- `Belt.List.headExn``Belt.List.headOrThrow`
5959
- `Belt.MutableQueue.peekExn``Belt.MutableQueue.peekOrThrow`
6060
- `Belt.MutableQueue.popExn``Belt.MutableQueue.popOrThrow`
61+
- `Belt.Option.getExn``Belt.Option.getOrThrow`
6162
- Old functions remain available but are marked as deprecated with guidance to use the new `OrThrow` variants.
6263
- https://github.com/rescript-lang/rescript/pull/7518, https://github.com/rescript-lang/rescript/pull/7554, https://github.com/rescript-lang/rescript/pull/7581
6364

analysis/reanalyze/src/ExnLib.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ let raisesLibTable : (Name.t, Exceptions.t) Hashtbl.t =
3131
in
3232
let beltSet = [("getExn", [notFound]); ("getOrThrow", [notFound])] in
3333
let beltMutableSet = beltSet in
34-
let beltOption = [("getExn", [notFound])] in
34+
let beltOption = [("getExn", [notFound]); ("getOrThrow", [notFound])] in
3535
let beltResult = [("getExn", [notFound])] in
3636
let bsJson =
3737
(* bs-json *)

runtime/Belt_Option.res

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ let forEach = (opt, f) =>
3434
| None => ()
3535
}
3636

37-
let getExn = x =>
37+
let getOrThrow = x =>
3838
switch x {
3939
| Some(x) => x
4040
| None => throw(Not_found)
4141
}
4242

43+
let getExn = getOrThrow
44+
4345
external getUnsafe: option<'a> => 'a = "%identity"
4446

4547
let mapWithDefault = (opt, default, f) =>

runtime/Belt_Option.resi

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,27 @@ switch Belt.Option.getExn(None) { // Raises an exception
8989
}
9090
```
9191
*/
92+
@deprecated("Use `getOrThrow` instead")
9293
let getExn: option<'a> => 'a
9394

95+
/**
96+
Raises an Error in case `None` is provided. Use with care.
97+
98+
## Examples
99+
100+
```rescript
101+
Some(3)
102+
->Belt.Option.getOrThrow
103+
->assertEqual(3)
104+
105+
switch Belt.Option.getOrThrow(None) { // Raises an exception
106+
| exception _ => assert(true)
107+
| _ => assert(false)
108+
}
109+
```
110+
*/
111+
let getOrThrow: option<'a> => 'a
112+
94113
/**
95114
`getUnsafe(x)` returns `x`
96115

0 commit comments

Comments
 (0)