Skip to content

Commit 00d730d

Browse files
committed
Rename Stdlib functions from Exn to OrThrow
1 parent 35c8f0e commit 00d730d

File tree

8 files changed

+153
-10
lines changed

8 files changed

+153
-10
lines changed

runtime/Stdlib_List.res

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

109-
let headExn = x =>
109+
let headOrThrow = x =>
110110
switch x {
111111
| list{} => throw(Not_found)
112112
| list{x, ..._} => x
113113
}
114114

115+
let headExn = headOrThrow
116+
115117
let tail = x =>
116118
switch x {
117119
| list{} => None
118120
| list{_, ...xs} => Some(xs)
119121
}
120122

121-
let tailExn = x =>
123+
let tailOrThrow = x =>
122124
switch x {
123125
| list{} => throw(Not_found)
124126
| list{_, ...t} => t
125127
}
126128

129+
let tailExn = tailOrThrow
130+
127131
let add = (xs, x) => list{x, ...xs}
128132

129133
/* Assume `n >=0` */
@@ -156,13 +160,15 @@ let get = (x, n) =>
156160
nthAux(x, n)
157161
}
158162

159-
let getExn = (x, n) =>
163+
let getOrThrow = (x, n) =>
160164
if n < 0 {
161165
throw(Not_found)
162166
} else {
163167
nthAuxAssert(x, n)
164168
}
165169

170+
let getExn = getOrThrow
171+
166172
let rec partitionAux = (p, cell, precX, precY) =>
167173
switch cell {
168174
| list{} => ()

runtime/Stdlib_List.resi

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,29 @@ switch List.headExn(list{}) {
9090
- Raises an Error if list is empty.
9191
9292
*/
93+
@deprecated("Use `headOrThrow` instead")
9394
let headExn: list<'a> => 'a
9495

96+
/**
97+
`headOrThrow(list)` same as [`head`](#head).
98+
99+
## Examples
100+
101+
```rescript
102+
List.headOrThrow(list{1, 2, 3})->assertEqual(1)
103+
104+
switch List.headOrThrow(list{}) {
105+
| exception Not_found => assert(true)
106+
| _ => assert(false)
107+
}
108+
```
109+
110+
## Exceptions
111+
112+
- Raises an Error if list is empty.
113+
*/
114+
let headOrThrow: list<'a> => 'a
115+
95116
/**
96117
`tail(list)` returns `None` if `list` is empty, otherwise it returns `Some(tail)`
97118
where `tail` is everything except the first element of `list`.
@@ -124,8 +145,25 @@ switch List.tailExn(list{}) {
124145
125146
- Raises an Error if list is empty.
126147
*/
148+
@deprecated("Use `tailOrThrow` instead")
127149
let tailExn: list<'a> => list<'a>
128150

151+
/**
152+
`tailOrThrow(list)` same as [`tail`](#tail).
153+
Raises an Error if list is empty.
154+
155+
## Examples
156+
```res
157+
List.tailOrThrow(list{1, 2, 3})->assertEqual(list{2, 3})
158+
159+
switch List.tailOrThrow(list{}) {
160+
| _ => Console.log("never happens")
161+
| exception _ => Console.log("error")
162+
}
163+
```
164+
*/
165+
let tailOrThrow: list<'a> => list<'a>
166+
129167
/**
130168
`add(list, value)` adds a `value` to the beginning of list `list`.
131169
@@ -177,8 +215,26 @@ switch abc->List.getExn(4) {
177215
178216
- Raises an Error if `index` is larger than the length of list.
179217
*/
218+
@deprecated("Use `getOrThrow` instead")
180219
let getExn: (list<'a>, int) => 'a
181220

221+
/**
222+
`getOrThrow(list, index)` same as [`get`](#get).
223+
Raises an Error if `index` is larger than the length of list.
224+
225+
## Examples
226+
```res
227+
let abc = list{"A", "B", "C"}
228+
abc->List.getOrThrow(1)->assertEqual("B")
229+
230+
switch abc->List.getOrThrow(4) {
231+
| _ => Console.log("never happens")
232+
| exception _ => Console.log("error")
233+
}
234+
```
235+
*/
236+
let getOrThrow: (list<'a>, int) => 'a
237+
182238
/**
183239
`make(length, value)` returns a list of length `length` with each element filled
184240
with `value`. Returns an empty list if `value` is negative.

runtime/Stdlib_Null.res

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ let getOr = (value, default) =>
2929

3030
let getWithDefault = getOr
3131

32-
let getExn: t<'a> => 'a = value =>
32+
let getOrThrow: t<'a> => 'a = value =>
3333
switch value->toOption {
3434
| Some(x) => x
35-
| None => throw(Invalid_argument("Null.getExn: value is null"))
35+
| None => throw(Invalid_argument("Null.getOrThrow: value is null"))
3636
}
3737

38+
let getExn = getOrThrow
39+
3840
external getUnsafe: t<'a> => 'a = "%identity"
3941

4042
let forEach = (value, f) =>

runtime/Stdlib_Null.resi

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,34 @@ switch Null.getExn(%raw("null")) {
120120
121121
## Exceptions
122122
123-
- Raises `Invalid_argument` if `value` is `null`,
123+
- Raises `Invalid_argument` if `value` is `null`
124124
*/
125+
@deprecated("Use `getOrThrow` instead")
125126
let getExn: t<'a> => 'a
126127

128+
/**
129+
`getOrThrow(value)` raises an exception if `null`, otherwise returns the value.
130+
131+
```rescript
132+
Null.getOrThrow(Null.make(3))->assertEqual(3)
133+
134+
switch Null.getOrThrow(%raw("'ReScript'")) {
135+
| exception Invalid_argument(_) => assert(false)
136+
| value => assertEqual(value, "ReScript")
137+
}
138+
139+
switch Null.getOrThrow(%raw("null")) {
140+
| exception Invalid_argument(_) => assert(true)
141+
| _ => assert(false)
142+
}
143+
```
144+
145+
## Exceptions
146+
147+
- Raises `Invalid_argument` if `value` is `null`
148+
*/
149+
let getOrThrow: t<'a> => 'a
150+
127151
/**
128152
`getUnsafe(value)` returns `value`.
129153

runtime/Stdlib_Nullable.res

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ let getOr = (value, default) =>
3232

3333
let getWithDefault = getOr
3434

35-
let getExn: t<'a> => 'a = value =>
35+
let getOrThrow: t<'a> => 'a = value =>
3636
switch value->toOption {
3737
| Some(x) => x
38-
| None => throw(Invalid_argument("Nullable.getExn: value is null or undefined"))
38+
| None => throw(Invalid_argument("Nullable.getOrThrow: value is null or undefined"))
3939
}
4040

41+
let getExn = getOrThrow
42+
4143
external getUnsafe: t<'a> => 'a = "%identity"
4244

4345
let forEach = (value, f) =>

runtime/Stdlib_Nullable.resi

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,35 @@ switch Nullable.getExn(%raw("undefined")) {
157157
158158
- Raises `Invalid_argument` if `value` is `null` or `undefined`
159159
*/
160+
@deprecated("Use `getOrThrow` instead")
160161
let getExn: t<'a> => 'a
161162

163+
/**
164+
`getOrThrow(value)` raises an exception if `null` or `undefined`, otherwise returns the value.
165+
166+
```rescript
167+
switch Nullable.getOrThrow(%raw("'Hello'")) {
168+
| exception Invalid_argument(_) => assert(false)
169+
| value => assertEqual(value, "Hello")
170+
}
171+
172+
switch Nullable.getOrThrow(%raw("null")) {
173+
| exception Invalid_argument(_) => assert(true)
174+
| _ => assert(false)
175+
}
176+
177+
switch Nullable.getOrThrow(%raw("undefined")) {
178+
| exception Invalid_argument(_) => assert(true)
179+
| _ => assert(false)
180+
}
181+
```
182+
183+
## Exceptions
184+
185+
- Raises `Invalid_argument` if `value` is `null` or `undefined`
186+
*/
187+
let getOrThrow: t<'a> => 'a
188+
162189
/**
163190
`getUnsafe(value)` returns `value`.
164191

runtime/Stdlib_Option.res

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,20 @@ let forEach = (opt, f) =>
3535
| None => ()
3636
}
3737

38-
let getExn = (x, ~message=?) =>
38+
let getOrThrow = (x, ~message=?) =>
3939
switch x {
4040
| Some(x) => x
4141
| None =>
4242
Stdlib_Error.panic(
4343
switch message {
44-
| None => "Option.getExn called for None value"
44+
| None => "Option.getOrThrow called for None value"
4545
| Some(message) => message
4646
},
4747
)
4848
}
4949

50+
let getExn = getOrThrow
51+
5052
external getUnsafe: option<'a> => 'a = "%identity"
5153

5254
let mapOr = (opt, default, f) =>

runtime/Stdlib_Option.resi

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,32 @@ switch Option.getExn(None, ~message="was None!") {
9191
9292
- Raises an error if `opt` is `None`
9393
*/
94+
@deprecated("Use `getOrThrow` instead")
9495
let getExn: (option<'a>, ~message: string=?) => 'a
9596

97+
/**
98+
`getOrThrow(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.
99+
100+
```rescript
101+
Option.getOrThrow(Some(3))->assertEqual(3)
102+
103+
switch Option.getOrThrow(None) {
104+
| exception _ => assert(true)
105+
| _ => assert(false)
106+
}
107+
108+
switch Option.getOrThrow(None, ~message="was None!") {
109+
| exception _ => assert(true) // Raises an Error with the message "was None!"
110+
| _ => assert(false)
111+
}
112+
```
113+
114+
## Exceptions
115+
116+
- Raises an error if `opt` is `None`
117+
*/
118+
let getOrThrow: (option<'a>, ~message: string=?) => 'a
119+
96120
/**
97121
`getUnsafe(opt)` returns `value` if `opt` is `Some(value)`, otherwise `undefined`.
98122

0 commit comments

Comments
 (0)