Skip to content

Commit 7ea4cea

Browse files
committed
Add Belt.List.getOrThrow, headOrThrow and tailOrThrow
1 parent b360c59 commit 7ea4cea

File tree

4 files changed

+76
-4
lines changed

4 files changed

+76
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@
5353
- `Belt.MutableMap.getExn``Belt.MutableMap.getOrThrow`
5454
- `Belt.Set.getExn``Belt.Set.getOrThrow`
5555
- `Belt.MutableSet.getExn``Belt.MutableSet.getOrThrow`
56+
- `Belt.List.getExn``Belt.List.getOrThrow`
57+
- `Belt.List.tailExn``Belt.List.tailOrThrow`
58+
- `Belt.List.headExn``Belt.List.headOrThrow`
5659
- Old functions remain available but are marked as deprecated with guidance to use the new `OrThrow` variants.
5760
- https://github.com/rescript-lang/rescript/pull/7518, https://github.com/rescript-lang/rescript/pull/7554, https://github.com/rescript-lang/rescript/pull/7581
5861

analysis/reanalyze/src/ExnLib.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ let raisesLibTable : (Name.t, Exceptions.t) Hashtbl.t =
1010
]
1111
in
1212
let beltList =
13-
[("getExn", [notFound]); ("headExn", [notFound]); ("tailExn", [notFound])]
13+
[
14+
("getExn", [notFound]);
15+
("getOrThrow", [notFound]);
16+
("headExn", [notFound]);
17+
("headOrThrow", [notFound]);
18+
("tailExn", [notFound]);
19+
("tailOrThrow", [notFound]);
20+
]
1421
in
1522
let beltMap = [("getExn", [notFound]); ("getOrThrow", [notFound])] in
1623
let beltMutableMap = beltMap in

runtime/Belt_List.res

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,24 +86,28 @@ let head = x =>
8686
| list{x, ..._} => Some(x)
8787
}
8888

89-
let headExn = x =>
89+
let headOrThrow = x =>
9090
switch x {
9191
| list{} => throw(Not_found)
9292
| list{x, ..._} => x
9393
}
9494

95+
let headExn = headOrThrow
96+
9597
let tail = x =>
9698
switch x {
9799
| list{} => None
98100
| list{_, ...xs} => Some(xs)
99101
}
100102

101-
let tailExn = x =>
103+
let tailOrThrow = x =>
102104
switch x {
103105
| list{} => throw(Not_found)
104106
| list{_, ...t} => t
105107
}
106108

109+
let tailExn = tailOrThrow
110+
107111
let add = (xs, x) => list{x, ...xs}
108112

109113
/* Assume `n >=0` */
@@ -136,13 +140,15 @@ let get = (x, n) =>
136140
nthAux(x, n)
137141
}
138142

139-
let getExn = (x, n) =>
143+
let getOrThrow = (x, n) =>
140144
if n < 0 {
141145
throw(Not_found)
142146
} else {
143147
nthAuxAssert(x, n)
144148
}
145149

150+
let getExn = getOrThrow
151+
146152
let rec partitionAux = (p, cell, precX, precY) =>
147153
switch cell {
148154
| list{} => ()

runtime/Belt_List.resi

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,26 @@ switch Belt.List.headExn(list{}) { // Raises an Error
7777
}
7878
```
7979
*/
80+
@deprecated("Use `headOrThrow` instead")
8081
let headExn: t<'a> => 'a
8182

83+
/**
84+
Same as `Belt.List.head` but raises an exception if `someList` is empty. Use
85+
with care.
86+
87+
## Examples
88+
89+
```rescript
90+
Belt.List.headOrThrow(list{1, 2, 3})->assertEqual(1)
91+
92+
switch Belt.List.headOrThrow(list{}) { // Raises an Error
93+
| exception _ => assert(true)
94+
| _ => assert(false)
95+
}
96+
```
97+
*/
98+
let headOrThrow: t<'a> => 'a
99+
82100
/**
83101
Returns `None` if `someList` is empty, otherwise it returns `Some(tail)`
84102
where `tail` is everything except the first element of `someList`.
@@ -108,8 +126,26 @@ switch Belt.List.tailExn(list{}) { // Raises an Error
108126
}
109127
```
110128
*/
129+
@deprecated("Use `tailOrThrow` instead")
111130
let tailExn: t<'a> => t<'a>
112131

132+
/**
133+
Same as `Belt.List.tail` but raises an exception if `someList` is empty. Use
134+
with care.
135+
136+
## Examples
137+
138+
```rescript
139+
Belt.List.tailOrThrow(list{1, 2, 3})->assertEqual(list{2, 3})
140+
141+
switch Belt.List.tailOrThrow(list{}) { // Raises an Error
142+
| exception _ => assert(true)
143+
| _ => assert(false)
144+
}
145+
```
146+
*/
147+
let tailOrThrow: t<'a> => t<'a>
148+
113149
/**
114150
Adds `value` to the beginning of `someList`.
115151
@@ -156,8 +192,28 @@ switch abc->Belt.List.getExn(4) { // Raises an Error
156192
}
157193
```
158194
*/
195+
@deprecated("Use `getOrThrow` instead")
159196
let getExn: (t<'a>, int) => 'a
160197

198+
/**
199+
Same as `Belt.List.get` but raises an exception if `index` is larger than the
200+
length. Use with care.
201+
202+
## Examples
203+
204+
```rescript
205+
let abc = list{"A", "B", "C"}
206+
207+
abc->Belt.List.getOrThrow(1)->assertEqual("B")
208+
209+
switch abc->Belt.List.getOrThrow(4) { // Raises an Error
210+
| exception _ => assert(true)
211+
| _ => assert(false)
212+
}
213+
```
214+
*/
215+
let getOrThrow: (t<'a>, int) => 'a
216+
161217
/**
162218
Returns a list of length `numItems` with each element filled with value `v`. Returns an empty list if `numItems` is negative.
163219

0 commit comments

Comments
 (0)