Skip to content

Commit f97dd8d

Browse files
authored
Rename functions ending with Exn to OrThrow (#7518)
* Rename functions ending with `Exn` to `OrThrow` * Update reanalize tests * Final fixes
1 parent a5b8f7a commit f97dd8d

File tree

18 files changed

+222
-59
lines changed

18 files changed

+222
-59
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
1313
# 12.0.0-alpha.14 (Unreleased)
1414

15+
#### :boom: Breaking Change
16+
17+
- Rename functions ending with `Exn` to end with `OrThrow`. The old `Exn` functions are now deprecated:
18+
- `Bool.fromStringExn``Bool.fromStringOrThrow`
19+
- `BigInt.fromStringExn``BigInt.fromStringOrThrow`
20+
- `JSON.parseExn``JSON.parseOrThrow`
21+
- Changed `BigInt.fromFloat` to return an option rather than throwing an error.
22+
- Added `BigInt.fromFloatOrThrow`
23+
- Old functions remain available but are marked as deprecated with guidance to use the new `OrThrow` variants.
24+
1525
#### :rocket: New Feature
1626

1727
- Add `RegExp.flags`. https://github.com/rescript-lang/rescript/pull/7461

analysis/reanalyze/src/ExnLib.ml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,19 @@ let raisesLibTable : (Name.t, Exceptions.t) Hashtbl.t =
5050
("float_of_string", [failure]);
5151
]
5252
in
53-
let stdlibBigInt = [("fromStringExn", [jsExn])] in
54-
let stdlibBool = [("fromStringExn", [invalidArgument])] in
53+
let stdlibBigInt =
54+
[
55+
("fromStringExn", [jsExn]);
56+
("fromStringOrThrow", [jsExn]);
57+
("fromFloatOrThrow", [jsExn]);
58+
]
59+
in
60+
let stdlibBool =
61+
[
62+
("fromStringExn", [invalidArgument]);
63+
("fromStringOrThrow", [invalidArgument]);
64+
]
65+
in
5566
let stdlibError = [("raise", [jsExn])] in
5667
let stdlibExn =
5768
[
@@ -68,6 +79,7 @@ let raisesLibTable : (Name.t, Exceptions.t) Hashtbl.t =
6879
[
6980
("parseExn", [jsExn]);
7081
("parseExnWithReviver", [jsExn]);
82+
("parseOrThrow", [jsExn]);
7183
("stringifyAny", [jsExn]);
7284
("stringifyAnyWithIndent", [jsExn]);
7385
("stringifyAnyWithReplacer", [jsExn]);

lib/es6/Stdlib_BigInt.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11

22

33

4+
function fromString(value) {
5+
try {
6+
return BigInt(value);
7+
} catch (exn) {
8+
return;
9+
}
10+
}
11+
412
function fromFloat(value) {
513
try {
614
return BigInt(value);
@@ -14,6 +22,7 @@ function toInt(t) {
1422
}
1523

1624
export {
25+
fromString,
1726
fromFloat,
1827
toInt,
1928
}

lib/es6/Stdlib_Bool.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function fromString(s) {
2020
}
2121
}
2222

23-
function fromStringExn(param) {
23+
function fromStringOrThrow(param) {
2424
switch (param) {
2525
case "false" :
2626
return false;
@@ -29,15 +29,18 @@ function fromStringExn(param) {
2929
default:
3030
throw {
3131
RE_EXN_ID: "Invalid_argument",
32-
_1: "Bool.fromStringExn: value is neither \"true\" nor \"false\"",
32+
_1: "Bool.fromStringOrThrow: value is neither \"true\" nor \"false\"",
3333
Error: new Error()
3434
};
3535
}
3636
}
3737

38+
let fromStringExn = fromStringOrThrow;
39+
3840
export {
3941
toString,
4042
fromString,
43+
fromStringOrThrow,
4144
fromStringExn,
4245
}
4346
/* No side effect */

lib/js/Stdlib_BigInt.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
'use strict';
22

33

4+
function fromString(value) {
5+
try {
6+
return BigInt(value);
7+
} catch (exn) {
8+
return;
9+
}
10+
}
11+
412
function fromFloat(value) {
513
try {
614
return BigInt(value);
@@ -13,6 +21,7 @@ function toInt(t) {
1321
return Number(t) | 0;
1422
}
1523

24+
exports.fromString = fromString;
1625
exports.fromFloat = fromFloat;
1726
exports.toInt = toInt;
1827
/* No side effect */

lib/js/Stdlib_Bool.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function fromString(s) {
2020
}
2121
}
2222

23-
function fromStringExn(param) {
23+
function fromStringOrThrow(param) {
2424
switch (param) {
2525
case "false" :
2626
return false;
@@ -29,13 +29,16 @@ function fromStringExn(param) {
2929
default:
3030
throw {
3131
RE_EXN_ID: "Invalid_argument",
32-
_1: "Bool.fromStringExn: value is neither \"true\" nor \"false\"",
32+
_1: "Bool.fromStringOrThrow: value is neither \"true\" nor \"false\"",
3333
Error: new Error()
3434
};
3535
}
3636
}
3737

38+
let fromStringExn = fromStringOrThrow;
39+
3840
exports.toString = toString;
3941
exports.fromString = fromString;
42+
exports.fromStringOrThrow = fromStringOrThrow;
4043
exports.fromStringExn = fromStringExn;
4144
/* No side effect */

runtime/Stdlib_BigInt.res

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,89 @@ type t = bigint
66
@val external asIntN: (~width: int, bigint) => bigint = "BigInt.asIntN"
77
@val external asUintN: (~width: int, bigint) => bigint = "BigInt.asUintN"
88

9-
@val external fromString: string => bigint = "BigInt"
10-
119
/**
1210
Parses the given `string` into a `bigint` using JavaScript semantics. Return the
13-
number as a `bigint` if successfully parsed. Uncaught syntax exception otherwise.
11+
number as a `bigint` if successfully parsed. Throws a syntax exception otherwise.
1412
1513
## Examples
1614
1715
```rescript
18-
BigInt.fromStringExn("123")->assertEqual(123n)
16+
BigInt.fromStringOrThrow("123")->assertEqual(123n)
1917
20-
BigInt.fromStringExn("")->assertEqual(0n)
18+
BigInt.fromStringOrThrow("")->assertEqual(0n)
2119
22-
BigInt.fromStringExn("0x11")->assertEqual(17n)
20+
BigInt.fromStringOrThrow("0x11")->assertEqual(17n)
2321
24-
BigInt.fromStringExn("0b11")->assertEqual(3n)
22+
BigInt.fromStringOrThrow("0b11")->assertEqual(3n)
2523
26-
BigInt.fromStringExn("0o11")->assertEqual(9n)
24+
BigInt.fromStringOrThrow("0o11")->assertEqual(9n)
2725
2826
/* catch exception */
29-
switch BigInt.fromStringExn("a") {
27+
switch BigInt.fromStringOrThrow("a") {
3028
| exception JsExn(_error) => assert(true)
3129
| _bigInt => assert(false)
3230
}
3331
```
3432
*/
3533
@val
34+
external fromStringOrThrow: string => bigint = "BigInt"
35+
36+
/**
37+
Parses the given `string` into a `bigint` using JavaScript semantics. Returns
38+
`Some(bigint)` if the string can be parsed, `None` otherwise.
39+
40+
## Examples
41+
42+
```rescript
43+
BigInt.fromString("123")->assertEqual(Some(123n))
44+
45+
BigInt.fromString("")->assertEqual(Some(0n))
46+
47+
BigInt.fromString("0x11")->assertEqual(Some(17n))
48+
49+
BigInt.fromString("0b11")->assertEqual(Some(3n))
50+
51+
BigInt.fromString("0o11")->assertEqual(Some(9n))
52+
53+
BigInt.fromString("invalid")->assertEqual(None)
54+
```
55+
*/
56+
let fromString = (value: string) => {
57+
try Some(fromStringOrThrow(value)) catch {
58+
| _ => None
59+
}
60+
}
61+
62+
@deprecated("Use `fromStringOrThrow` instead") @val
3663
external fromStringExn: string => bigint = "BigInt"
64+
3765
@val external fromInt: int => bigint = "BigInt"
38-
@val external fromFloat: float => bigint = "BigInt"
66+
67+
/**
68+
Converts a `float` to a `bigint` using JavaScript semantics.
69+
Throws an exception if the float is not an integer or is infinite/NaN.
70+
71+
## Examples
72+
73+
```rescript
74+
BigInt.fromFloatOrThrow(123.0)->assertEqual(123n)
75+
76+
BigInt.fromFloatOrThrow(0.0)->assertEqual(0n)
77+
78+
BigInt.fromFloatOrThrow(-456.0)->assertEqual(-456n)
79+
80+
/* This will throw an exception */
81+
switch BigInt.fromFloatOrThrow(123.5) {
82+
| exception JsExn(_error) => assert(true)
83+
| _bigInt => assert(false)
84+
}
85+
```
86+
*/
87+
@val
88+
external fromFloatOrThrow: float => bigint = "BigInt"
3989

4090
let fromFloat = (value: float) => {
41-
try Some(fromFloat(value)) catch {
91+
try Some(fromFloatOrThrow(value)) catch {
4292
| _ => None
4393
}
4494
}

runtime/Stdlib_Bool.res

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ let fromString = s => {
1515
}
1616
}
1717

18-
let fromStringExn = param =>
18+
let fromStringOrThrow = param =>
1919
switch param {
2020
| "true" => true
2121
| "false" => false
22-
| _ => throw(Invalid_argument(`Bool.fromStringExn: value is neither "true" nor "false"`))
22+
| _ => throw(Invalid_argument(`Bool.fromStringOrThrow: value is neither "true" nor "false"`))
2323
}
2424

25+
@deprecated("Use `fromStringOrThrow` instead")
26+
let fromStringExn = fromStringOrThrow
27+
2528
external compare: (bool, bool) => Stdlib_Ordering.t = "%compare"
2629

2730
external equal: (bool, bool) => bool = "%equal"

runtime/Stdlib_Bool.resi

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@ Bool.fromString("notAValidBoolean")->assertEqual(None)
3131
*/
3232
let fromString: string => option<bool>
3333

34+
/**
35+
Converts a string to a boolean.
36+
Throws an `Invalid_argument` exception if the string is not a valid boolean.
37+
38+
## Examples
39+
```rescript
40+
Bool.fromStringOrThrow("true")->assertEqual(true)
41+
Bool.fromStringOrThrow("false")->assertEqual(false)
42+
switch Bool.fromStringOrThrow("notAValidBoolean") {
43+
| exception Invalid_argument(_) => assert(true)
44+
| _ => assert(false)
45+
}
46+
```
47+
*/
48+
let fromStringOrThrow: string => bool
49+
3450
/**
3551
Converts a string to a boolean.
3652
Beware, this function will throw an `Invalid_argument` exception
@@ -46,6 +62,7 @@ switch Bool.fromStringExn("notAValidBoolean") {
4662
}
4763
```
4864
*/
65+
@deprecated("Use `fromStringOrThrow` instead")
4966
let fromStringExn: string => bool
5067

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

runtime/Stdlib_JSON.res

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ type rec t =
1010
@unboxed
1111
type replacer = Keys(array<string>) | Replacer((string, t) => t)
1212

13-
@raises @val external parseExn: (string, ~reviver: (string, t) => t=?) => t = "JSON.parse"
14-
@deprecated("Use `parseExn` with optional parameter instead") @raises @val
13+
@raises @val external parseOrThrow: (string, ~reviver: (string, t) => t=?) => t = "JSON.parse"
14+
15+
@deprecated("Use `parseOrThrow` instead") @raises @val
16+
external parseExn: (string, ~reviver: (string, t) => t=?) => t = "JSON.parse"
17+
18+
@deprecated("Use `parseOrThrow` with optional parameter instead") @raises @val
1519
external parseExnWithReviver: (string, (string, t) => t) => t = "JSON.parse"
1620

1721
@val external stringify: (t, ~replacer: replacer=?, ~space: int=?) => string = "JSON.stringify"

0 commit comments

Comments
 (0)