Skip to content

Commit 6237510

Browse files
committed
Add Belt.Result.getOrThrow
1 parent f3cc3c3 commit 6237510

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

CHANGELOG.md

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

analysis/reanalyze/src/ExnLib.ml

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

runtime/Belt_Result.res

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ type t<'a, 'b> = result<'a, 'b> =
2626
| Ok('a)
2727
| Error('b)
2828

29-
let getExn = x =>
29+
let getOrThrow = x =>
3030
switch x {
3131
| Ok(x) => x
3232
| Error(_) => throw(Not_found)
3333
}
3434

35+
let getExn = getOrThrow
36+
3537
let mapWithDefault = (opt, default, f) =>
3638
switch opt {
3739
| Ok(x) => f(x)

runtime/Belt_Result.resi

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,28 @@ switch Belt.Result.getExn(Belt.Result.Error("Invalid data")) { // raise a except
5050
}
5151
```
5252
*/
53+
@deprecated("Use `getOrThrow` instead")
5354
let getExn: t<'a, 'b> => 'a
5455

56+
/**
57+
`getOrThrow(res)`: when `res` is `Ok(n)`, returns `n` when `res` is `Error(m)`, raise an exception
58+
59+
## Examples
60+
61+
```rescript
62+
Belt.Result.Ok(42)
63+
->Belt.Result.getOrThrow
64+
->assertEqual(42)
65+
66+
67+
switch Belt.Result.getOrThrow(Belt.Result.Error("Invalid data")) { // raise a exception
68+
| exception _ => assert(true)
69+
| _ => assert(false)
70+
}
71+
```
72+
*/
73+
let getOrThrow: t<'a, 'b> => 'a
74+
5575
@deprecated("Use `mapWithDefault` instead")
5676
let mapWithDefaultU: (t<'a, 'c>, 'b, 'a => 'b) => 'b
5777
/**

0 commit comments

Comments
 (0)