Skip to content

Commit 6654cb2

Browse files
aspeddrocknitt
andauthored
Add throw (#7346)
* Add `throw` Deprecate `raise` and rename references * remove raise from Pervasives_mini.res * convert to external and add deprecated attr * restore tests/gentype_tests/typescript-react-example/package-lock.json * update analysis output * update CHANGELOG.md * move `throw` to Pervasives.res * Update runtime/Pervasives.res Co-authored-by: Christoph Knittel <[email protected]> * Update runtime/Pervasives.res Co-authored-by: Christoph Knittel <[email protected]> * Update runtime/Pervasives.res Co-authored-by: Christoph Knittel <[email protected]> * Update runtime/Stdlib_Error.resi Co-authored-by: Christoph Knittel <[email protected]> * Update runtime/Stdlib_Error.resi Co-authored-by: Christoph Knittel <[email protected]> * Update runtime/Stdlib_Error.resi Co-authored-by: Christoph Knittel <[email protected]> * update Stdlib_Bool and Stdlib_Char * remove comment --------- Co-authored-by: Christoph Knittel <[email protected]>
1 parent 12f2578 commit 6654cb2

31 files changed

+91
-57
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- Fix Pervasive.max using boolean comparison for floats. https://github.com/rescript-lang/rescript/pull/7333
2323
- Experimental: Support nested/inline record types - records defined inside of other records, without needing explicit separate type definitions. https://github.com/rescript-lang/rescript/pull/7241
2424
- Add unified exponentiation (`**`) operator for numeric types using ES7 `**`. https://github.com/rescript-lang/rescript-compiler/pull/7153
25+
- Rename `raise` to `throw` to align with JavaScript vocabulary. `raise` has been deprecated. https://github.com/rescript-lang/rescript/pull/7346
2526

2627
#### :boom: Breaking Change
2728

runtime/Belt_List.res

+4-4
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ let head = x =>
8888

8989
let headExn = x =>
9090
switch x {
91-
| list{} => raise(Not_found)
91+
| list{} => throw(Not_found)
9292
| list{x, ..._} => x
9393
}
9494

@@ -100,7 +100,7 @@ let tail = x =>
100100

101101
let tailExn = x =>
102102
switch x {
103-
| list{} => raise(Not_found)
103+
| list{} => throw(Not_found)
104104
| list{_, ...t} => t
105105
}
106106

@@ -126,7 +126,7 @@ let rec nthAuxAssert = (x, n) =>
126126
} else {
127127
nthAuxAssert(t, n - 1)
128128
}
129-
| _ => raise(Not_found)
129+
| _ => throw(Not_found)
130130
}
131131

132132
let get = (x, n) =>
@@ -138,7 +138,7 @@ let get = (x, n) =>
138138

139139
let getExn = (x, n) =>
140140
if n < 0 {
141-
raise(Not_found)
141+
throw(Not_found)
142142
} else {
143143
nthAuxAssert(x, n)
144144
}

runtime/Belt_MutableQueue.res

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ let peekUndefined = q =>
7272

7373
let peekExn = q =>
7474
switch q.first {
75-
| None => raise(Not_found)
75+
| None => throw(Not_found)
7676
| Some(v) => v.content
7777
}
7878

@@ -95,7 +95,7 @@ let pop = q =>
9595
let popExn = q =>
9696
/* TO fix */
9797
switch q.first {
98-
| None => raise(Not_found)
98+
| None => throw(Not_found)
9999
| Some(x) =>
100100
let next = x.next
101101
if next == None {

runtime/Belt_Option.res

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ let forEach = (opt, f) =>
3737
let getExn = x =>
3838
switch x {
3939
| Some(x) => x
40-
| None => raise(Not_found)
40+
| None => throw(Not_found)
4141
}
4242

4343
external getUnsafe: option<'a> => 'a = "%identity"

runtime/Belt_Result.res

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type t<'a, 'b> = result<'a, 'b> =
2929
let getExn = x =>
3030
switch x {
3131
| Ok(x) => x
32-
| Error(_) => raise(Not_found)
32+
| Error(_) => throw(Not_found)
3333
}
3434

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

runtime/Belt_internalAVLset.res

+1-1
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ let rec getUndefined = (n: t<_>, x, ~cmp) =>
590590

591591
let rec getExn = (n: t<_>, x, ~cmp) =>
592592
switch n {
593-
| None => raise(Not_found)
593+
| None => throw(Not_found)
594594
| Some(t) /* Node(l, v, r, _) */ =>
595595
let v = t.value
596596
let c = Belt_Id.getCmpInternal(cmp)(x, v)

runtime/Belt_internalAVLtree.res

+1-1
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ let rec getUndefined = (n, x, ~cmp) =>
713713

714714
let rec getExn = (n, x, ~cmp) =>
715715
switch n {
716-
| None => raise(Not_found)
716+
| None => throw(Not_found)
717717
| Some(n) /* Node(l, v, d, r, _) */ =>
718718
let v = n.key
719719
let c = Belt_Id.getCmpInternal(cmp)(x, v)

runtime/Belt_internalMapInt.res

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ let rec getUndefined = (n, x: key) =>
6565

6666
let rec getExn = (n, x: key) =>
6767
switch n {
68-
| None => raise(Not_found)
68+
| None => throw(Not_found)
6969
| Some(n) =>
7070
let v = n.N.key
7171
if x == v {

runtime/Belt_internalMapString.res

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ let rec getUndefined = (n, x: key) =>
6565

6666
let rec getExn = (n, x: key) =>
6767
switch n {
68-
| None => raise(Not_found)
68+
| None => throw(Not_found)
6969
| Some(n) =>
7070
let v = n.N.key
7171
if x == v {

runtime/Belt_internalSetInt.res

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ let rec getUndefined = (n: t, x: value) =>
106106

107107
let rec getExn = (n: t, x: value) =>
108108
switch n {
109-
| None => raise(Not_found)
109+
| None => throw(Not_found)
110110
| Some(t) =>
111111
let v = t.value
112112
if x == v {

runtime/Belt_internalSetString.res

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ let rec getUndefined = (n: t, x: value) =>
106106

107107
let rec getExn = (n: t, x: value) =>
108108
switch n {
109-
| None => raise(Not_found)
109+
| None => throw(Not_found)
110110
| Some(t) =>
111111
let v = t.value
112112
if x == v {

runtime/Pervasives.res

+22-11
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
1-
/**
2-
Since [others] depend on this file, its public mli files **should not
3-
export types** introduced here, otherwise it would cause
4-
conflicts here.
5-
6-
If the type exported here is also exported in modules from others,
7-
you will get a type not equivalent.
8-
*/
91
@deprecated("Do not use. This will be removed in v13")
102
external /* Internal */
113

124
__unsafe_cast: 'a => 'b = "%identity"
135

146
/* Exceptions */
15-
7+
@deprecated(
8+
"`raise` has been renamed to `throw` to align with JavaScript vocabulary. Please use `throw` instead"
9+
)
1610
external raise: exn => 'a = "%raise"
1711

1812
@deprecated("Use custom exception instead")
19-
let failwith = s => raise(Failure(s))
13+
let failwith = s => throw(Failure(s))
2014

2115
@deprecated("Use custom exception instead")
22-
let invalid_arg = s => raise(Invalid_argument(s))
16+
let invalid_arg = s => throw(Invalid_argument(s))
2317

2418
@deprecated("Use custom exception instead") exception Exit
2519

20+
/**
21+
Raises the given exception, terminating execution unless caught by a surrounding try/catch block.
22+
23+
## Examples
24+
25+
```rescript
26+
let error = Error.make("Everything is upside down.")
27+
28+
if 5 > 10 {
29+
throw(error)
30+
} else {
31+
Console.log("Phew, sanity still rules.")
32+
}
33+
```
34+
*/
35+
external throw: Stdlib_Error.t => 'a = "%raise"
36+
2637
/* Composition operators */
2738

2839
external \"|>": ('a, 'a => 'b) => 'b = "%revapply"

runtime/Pervasives_mini.res

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* Exceptions */
2-
external raise: exn => 'a = "%raise"
2+
external throw: exn => 'a = "%raise"
33

44
/* Debugging */
55

runtime/Primitive_array.res

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ let length = Primitive_array_extern.length
22

33
let get = (xs, index) =>
44
if index < 0 || index >= length(xs) {
5-
raise(Invalid_argument("index out of bounds"))
5+
throw(Invalid_argument("index out of bounds"))
66
} else {
77
xs->Primitive_array_extern.getUnsafe(index)
88
}
99

1010
let set = (xs, index, newval) =>
1111
if index < 0 || index >= length(xs) {
12-
raise(Invalid_argument("index out of bounds"))
12+
throw(Invalid_argument("index out of bounds"))
1313
} else {
1414
xs->Primitive_array_extern.setUnsafe(index, newval)
1515
}

runtime/Primitive_bigint.res

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ external div: (bigint, bigint) => bigint = "%divbigint"
2525

2626
let div = (x: bigint, y: bigint) =>
2727
if y == 0n {
28-
raise(Division_by_zero)
28+
throw(Division_by_zero)
2929
} else {
3030
div(x, y)
3131
}
@@ -34,7 +34,7 @@ external mod_: (bigint, bigint) => bigint = "%modbigint"
3434

3535
let mod_ = (x: bigint, y: bigint) =>
3636
if y == 0n {
37-
raise(Division_by_zero)
37+
throw(Division_by_zero)
3838
} else {
3939
mod_(x, y)
4040
}

runtime/Primitive_int.res

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ let max = (x: int, y: int): int =>
2323

2424
let div = (x: int, y: int) =>
2525
if y == 0 {
26-
raise(Division_by_zero)
26+
throw(Division_by_zero)
2727
} else {
2828
Primitive_int_extern.div(x, y)
2929
}
3030

3131
let mod_ = (x: int, y: int) =>
3232
if y == 0 {
33-
raise(Division_by_zero)
33+
throw(Division_by_zero)
3434
} else {
3535
Primitive_int_extern.mod_(x, y)
3636
}

runtime/Primitive_lazy.res

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ exception Undefined
5050
}
5151
)
5252

53-
%%private(let raise_undefined = () => raise(Undefined))
53+
%%private(let raise_undefined = () => throw(Undefined))
5454

5555
/* Assume [blk] is a block with tag lazy */
5656
%%private(
@@ -59,8 +59,8 @@ exception Undefined
5959
blk.value = fnToVal(raise_undefined)
6060
try forward_with_closure(blk, closure) catch {
6161
| e =>
62-
blk.value = fnToVal(() => raise(e))
63-
raise(e)
62+
blk.value = fnToVal(() => throw(e))
63+
throw(e)
6464
}
6565
}
6666
)

runtime/Primitive_module.res

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module type Empty = {}
1919
in the lambda layer
2020
*/
2121
let init = (loc: (string, int, int), shape: shape) => {
22-
let undef_module = _ => raise(Undefined_recursive_module(loc))
22+
let undef_module = _ => throw(Undefined_recursive_module(loc))
2323
let rec loop = (shape: shape, struct_: Obj.t, idx) =>
2424
switch shape {
2525
| Function => Obj.setField(struct_, idx, Obj.magic(undef_module))

runtime/Primitive_object.res

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ let rec compare = (a: t, b: t): int =>
9494
| ("boolean", "boolean") => Pervasives.compare((magic(a): bool), magic(b))
9595
| ("boolean", _) => 1
9696
| (_, "boolean") => -1
97-
| ("function", "function") => raise(Invalid_argument("compare: functional value"))
97+
| ("function", "function") => throw(Invalid_argument("compare: functional value"))
9898
| ("function", _) => 1
9999
| (_, "function") => -1
100100
| ("bigint", "bigint")
@@ -261,7 +261,7 @@ let rec equal = (a: t, b: t): bool =>
261261
} else {
262262
let b_type = Js.typeof(b)
263263
if a_type == "function" || b_type == "function" {
264-
raise(Invalid_argument("equal: functional value"))
264+
throw(Invalid_argument("equal: functional value"))
265265
} /* first, check using reference equality */
266266
else if (
267267
/* a_type = "object" || "symbol" */

runtime/Primitive_string.res

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ let max = (x: string, y: string): string =>
2323

2424
let getChar = (s, i) =>
2525
if i >= Primitive_string_extern.length(s) || i < 0 {
26-
raise(Invalid_argument("index out of bounds"))
26+
throw(Invalid_argument("index out of bounds"))
2727
} else {
2828
Primitive_string_extern.getChar(s, i)
2929
}

runtime/Primitive_util.res

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Js = Primitive_js_extern
22

33
let raiseWhenNotFound = x =>
44
if Js.testAny(x) {
5-
raise(Not_found)
5+
throw(Not_found)
66
} else {
77
x
88
}

runtime/Stdlib_Bool.res

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ let fromStringExn = param =>
1919
switch param {
2020
| "true" => true
2121
| "false" => false
22-
| _ => raise(Invalid_argument(`Bool.fromStringExn: value is neither "true" nor "false"`))
22+
| _ => throw(Invalid_argument(`Bool.fromStringExn: value is neither "true" nor "false"`))
2323
}
2424

2525
external compare: (bool, bool) => Stdlib_Ordering.t = "%compare"

runtime/Stdlib_Char.res

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ external fromIntUnsafe: int => t = "%identity"
1010

1111
let fromIntExn = n =>
1212
if n < 0 || n > 255 {
13-
raise(Invalid_argument("`Char.fromIntExn` expects an integer between 0 and 255"))
13+
throw(Invalid_argument("`Char.fromIntExn` expects an integer between 0 and 255"))
1414
} else {
1515
fromIntUnsafe(n)
1616
}

runtime/Stdlib_Error.res

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ module URIError = {
4040

4141
external raise: t => 'a = "%raise"
4242

43+
external throw: t => 'a = "%raise"
44+
4345
let panic = msg => make(`Panic! ${msg}`)->raise
4446

4547
external ignore: t => unit = "%ignore"

runtime/Stdlib_Error.resi

+20
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,28 @@ if 5 > 10 {
155155
}
156156
```
157157
*/
158+
@deprecated(
159+
"`raise` has been renamed to `throw` to align with JavaScript vocabulary. Please use `throw` instead"
160+
)
158161
external raise: t => 'a = "%raise"
159162

163+
/**
164+
Raises the given exception, terminating execution unless caught by a surrounding try/catch block.
165+
166+
## Examples
167+
168+
```rescript
169+
let error = Error.make("Everything is upside down.")
170+
171+
if 5 > 10 {
172+
Error.throw(error)
173+
} else {
174+
Console.log("Phew, sanity still rules.")
175+
}
176+
```
177+
*/
178+
external throw: t => 'a = "%raise"
179+
160180
/**
161181
Raises a panic exception with the given message.
162182

0 commit comments

Comments
 (0)