Skip to content

Commit 0f6d3e1

Browse files
committed
Add Belt.Array.getOrThrow and setOrThrow
1 parent 78415d5 commit 0f6d3e1

File tree

9 files changed

+61
-9
lines changed

9 files changed

+61
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
- `List.getExn``List.getOrThrow`
4848
- `List.tailExn``List.tailOrThrow`
4949
- `List.headExn``List.headOrThrow`
50+
- `Belt.Array.getExn``Belt.Array.getOrThrow`
51+
- `Belt.Array.setExn``Belt.Array.setOrThrow`
5052
- Old functions remain available but are marked as deprecated with guidance to use the new `OrThrow` variants.
5153
- https://github.com/rescript-lang/rescript/pull/7518, https://github.com/rescript-lang/rescript/pull/7554
5254

analysis/reanalyze/src/ExnLib.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
let raisesLibTable : (Name.t, Exceptions.t) Hashtbl.t =
22
let table = Hashtbl.create 15 in
33
let open Exn in
4-
let beltArray = [("getExn", [assertFailure]); ("setExn", [assertFailure])] in
4+
let beltArray =
5+
[
6+
("getExn", [assertFailure]);
7+
("getOrThrow", [assertFailure]);
8+
("setExn", [assertFailure]);
9+
("setOrThrow", [assertFailure]);
10+
]
11+
in
512
let beltList =
613
[("getExn", [notFound]); ("headExn", [notFound]); ("tailExn", [notFound])]
714
in

lib/es6/Belt_Array.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function get(arr, i) {
1010

1111
}
1212

13-
function getExn(arr, i) {
13+
function getOrThrow(arr, i) {
1414
if (!(i >= 0 && i < arr.length)) {
1515
throw {
1616
RE_EXN_ID: "Assert_failure",
@@ -34,13 +34,13 @@ function set(arr, i, v) {
3434
}
3535
}
3636

37-
function setExn(arr, i, v) {
37+
function setOrThrow(arr, i, v) {
3838
if (!(i >= 0 && i < arr.length)) {
3939
throw {
4040
RE_EXN_ID: "Assert_failure",
4141
_1: [
4242
"Belt_Array.res",
43-
49,
43+
51,
4444
2
4545
],
4646
Error: new Error()
@@ -582,6 +582,10 @@ function init(n, f) {
582582
return v;
583583
}
584584

585+
let getExn = getOrThrow;
586+
587+
let setExn = setOrThrow;
588+
585589
let makeByU = makeBy;
586590

587591
let makeByAndShuffleU = makeByAndShuffle;
@@ -637,8 +641,10 @@ let initU = init;
637641
export {
638642
get,
639643
getExn,
644+
getOrThrow,
640645
set,
641646
setExn,
647+
setOrThrow,
642648
shuffleInPlace,
643649
shuffle,
644650
reverseInPlace,

lib/js/Belt_Array.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function get(arr, i) {
1010

1111
}
1212

13-
function getExn(arr, i) {
13+
function getOrThrow(arr, i) {
1414
if (!(i >= 0 && i < arr.length)) {
1515
throw {
1616
RE_EXN_ID: "Assert_failure",
@@ -34,13 +34,13 @@ function set(arr, i, v) {
3434
}
3535
}
3636

37-
function setExn(arr, i, v) {
37+
function setOrThrow(arr, i, v) {
3838
if (!(i >= 0 && i < arr.length)) {
3939
throw {
4040
RE_EXN_ID: "Assert_failure",
4141
_1: [
4242
"Belt_Array.res",
43-
49,
43+
51,
4444
2
4545
],
4646
Error: new Error()
@@ -582,6 +582,10 @@ function init(n, f) {
582582
return v;
583583
}
584584

585+
let getExn = getOrThrow;
586+
587+
let setExn = setOrThrow;
588+
585589
let makeByU = makeBy;
586590

587591
let makeByAndShuffleU = makeByAndShuffle;
@@ -636,8 +640,10 @@ let initU = init;
636640

637641
exports.get = get;
638642
exports.getExn = getExn;
643+
exports.getOrThrow = getOrThrow;
639644
exports.set = set;
640645
exports.setExn = setExn;
646+
exports.setOrThrow = setOrThrow;
641647
exports.shuffleInPlace = shuffleInPlace;
642648
exports.shuffle = shuffle;
643649
exports.reverseInPlace = reverseInPlace;

rewatch/testrepo/packages/dep02/src/Array.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,14 @@ let get = Belt_Array.get;
291291

292292
let getExn = Belt_Array.getExn;
293293

294+
let getOrThrow = Belt_Array.getOrThrow;
295+
294296
let set = Belt_Array.set;
295297

296298
let setExn = Belt_Array.setExn;
297299

300+
let setOrThrow = Belt_Array.setOrThrow;
301+
298302
let shuffleInPlace = Belt_Array.shuffleInPlace;
299303

300304
let shuffle = Belt_Array.shuffle;
@@ -434,8 +438,10 @@ let sortByRaw = Belt_SortArray.stableSortBy;
434438
export {
435439
get,
436440
getExn,
441+
getOrThrow,
437442
set,
438443
setExn,
444+
setOrThrow,
439445
shuffleInPlace,
440446
shuffle,
441447
reverseInPlace,

runtime/Belt_Array.res

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ let get = (arr, i) =>
3232
None
3333
}
3434

35-
let getExn = (arr, i) => {
35+
let getOrThrow = (arr, i) => {
3636
assert(i >= 0 && i < length(arr))
3737
getUnsafe(arr, i)
3838
}
3939

40+
let getExn = getOrThrow
41+
4042
let set = (arr, i, v) =>
4143
if i >= 0 && i < length(arr) {
4244
setUnsafe(arr, i, v)
@@ -45,11 +47,13 @@ let set = (arr, i, v) =>
4547
false
4648
}
4749

48-
let setExn = (arr, i, v) => {
50+
let setOrThrow = (arr, i, v) => {
4951
assert(i >= 0 && i < length(arr))
5052
setUnsafe(arr, i, v)
5153
}
5254

55+
let setExn = setOrThrow
56+
5357
@set external truncateToLengthUnsafe: (t<'a>, int) => unit = "length"
5458

5559
@new external makeUninitialized: int => array<Js.undefined<'a>> = "Array"

runtime/Belt_Array.resi

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,15 @@ let get: (t<'a>, int) => option<'a>
5050
Raise an exception if `i` is out of range.
5151
Otherwise return the value at index `i` in `arr`.
5252
*/
53+
@deprecated("Use 'getOrThrow' instead")
5354
let getExn: (t<'a>, int) => 'a
5455

56+
/**
57+
Raise an exception if `i` is out of range.
58+
Otherwise return the value at index `i` in `arr`.
59+
*/
60+
let getOrThrow: (t<'a>, int) => 'a
61+
5562
/**
5663
`getUnsafe(arr, i)`
5764
@@ -79,8 +86,14 @@ let set: (t<'a>, int, 'a) => bool
7986
/**
8087
`setExn(arr, i, x)` raise an exception if `i` is out of range.
8188
*/
89+
@deprecated("Use 'setOrThrow' instead")
8290
let setExn: (t<'a>, int, 'a) => unit
8391

92+
/**
93+
`setOrThrow(arr, i, x)` raise an exception if `i` is out of range.
94+
*/
95+
let setOrThrow: (t<'a>, int, 'a) => unit
96+
8497
external setUnsafe: (t<'a>, int, 'a) => unit = "%array_unsafe_set"
8598

8699
/**

tests/tests/src/module_missing_conversion.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ function f(x) {
1010
let XX = {
1111
get: Belt_Array.get,
1212
getExn: Belt_Array.getExn,
13+
getOrThrow: Belt_Array.getOrThrow,
1314
set: Belt_Array.set,
1415
setExn: Belt_Array.setExn,
16+
setOrThrow: Belt_Array.setOrThrow,
1517
shuffleInPlace: Belt_Array.shuffleInPlace,
1618
shuffle: Belt_Array.shuffle,
1719
reverseInPlace: Belt_Array.reverseInPlace,

tests/tests/src/test_simple_include.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@ let get = Belt_Array.get;
2626

2727
let getExn = Belt_Array.getExn;
2828

29+
let getOrThrow = Belt_Array.getOrThrow;
30+
2931
let set = Belt_Array.set;
3032

3133
let setExn = Belt_Array.setExn;
3234

35+
let setOrThrow = Belt_Array.setOrThrow;
36+
3337
let shuffleInPlace = Belt_Array.shuffleInPlace;
3438

3539
let shuffle = Belt_Array.shuffle;
@@ -171,8 +175,10 @@ let a = 3;
171175
export {
172176
get,
173177
getExn,
178+
getOrThrow,
174179
set,
175180
setExn,
181+
setOrThrow,
176182
shuffleInPlace,
177183
shuffle,
178184
reverseInPlace,

0 commit comments

Comments
 (0)