diff --git a/CHANGELOG.md b/CHANGELOG.md index ecd526c481..f198b4f191 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ - Fix Pervasive.max using boolean comparison for floats. https://github.com/rescript-lang/rescript/pull/7333 - 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 - Add unified exponentiation (`**`) operator for numeric types using ES7 `**`. https://github.com/rescript-lang/rescript-compiler/pull/7153 +- Rename `raise` to `throw` to align with JavaScript vocabulary. `raise` has been deprecated. https://github.com/rescript-lang/rescript/pull/7346 #### :boom: Breaking Change diff --git a/runtime/Belt_List.res b/runtime/Belt_List.res index cf00c5611e..b282a596ac 100644 --- a/runtime/Belt_List.res +++ b/runtime/Belt_List.res @@ -88,7 +88,7 @@ let head = x => let headExn = x => switch x { - | list{} => raise(Not_found) + | list{} => throw(Not_found) | list{x, ..._} => x } @@ -100,7 +100,7 @@ let tail = x => let tailExn = x => switch x { - | list{} => raise(Not_found) + | list{} => throw(Not_found) | list{_, ...t} => t } @@ -126,7 +126,7 @@ let rec nthAuxAssert = (x, n) => } else { nthAuxAssert(t, n - 1) } - | _ => raise(Not_found) + | _ => throw(Not_found) } let get = (x, n) => @@ -138,7 +138,7 @@ let get = (x, n) => let getExn = (x, n) => if n < 0 { - raise(Not_found) + throw(Not_found) } else { nthAuxAssert(x, n) } diff --git a/runtime/Belt_MutableQueue.res b/runtime/Belt_MutableQueue.res index 855fc03bf2..40d39c319d 100644 --- a/runtime/Belt_MutableQueue.res +++ b/runtime/Belt_MutableQueue.res @@ -72,7 +72,7 @@ let peekUndefined = q => let peekExn = q => switch q.first { - | None => raise(Not_found) + | None => throw(Not_found) | Some(v) => v.content } @@ -95,7 +95,7 @@ let pop = q => let popExn = q => /* TO fix */ switch q.first { - | None => raise(Not_found) + | None => throw(Not_found) | Some(x) => let next = x.next if next == None { diff --git a/runtime/Belt_Option.res b/runtime/Belt_Option.res index 3d6b18ff5a..cd2f7e0596 100644 --- a/runtime/Belt_Option.res +++ b/runtime/Belt_Option.res @@ -37,7 +37,7 @@ let forEach = (opt, f) => let getExn = x => switch x { | Some(x) => x - | None => raise(Not_found) + | None => throw(Not_found) } external getUnsafe: option<'a> => 'a = "%identity" diff --git a/runtime/Belt_Result.res b/runtime/Belt_Result.res index a6ded4bff6..d85948a96d 100644 --- a/runtime/Belt_Result.res +++ b/runtime/Belt_Result.res @@ -29,7 +29,7 @@ type t<'a, 'b> = result<'a, 'b> = let getExn = x => switch x { | Ok(x) => x - | Error(_) => raise(Not_found) + | Error(_) => throw(Not_found) } let mapWithDefault = (opt, default, f) => diff --git a/runtime/Belt_internalAVLset.res b/runtime/Belt_internalAVLset.res index 422e30b9db..46954fd095 100644 --- a/runtime/Belt_internalAVLset.res +++ b/runtime/Belt_internalAVLset.res @@ -590,7 +590,7 @@ let rec getUndefined = (n: t<_>, x, ~cmp) => let rec getExn = (n: t<_>, x, ~cmp) => switch n { - | None => raise(Not_found) + | None => throw(Not_found) | Some(t) /* Node(l, v, r, _) */ => let v = t.value let c = Belt_Id.getCmpInternal(cmp)(x, v) diff --git a/runtime/Belt_internalAVLtree.res b/runtime/Belt_internalAVLtree.res index e47c211d41..cdb0dc67f0 100644 --- a/runtime/Belt_internalAVLtree.res +++ b/runtime/Belt_internalAVLtree.res @@ -713,7 +713,7 @@ let rec getUndefined = (n, x, ~cmp) => let rec getExn = (n, x, ~cmp) => switch n { - | None => raise(Not_found) + | None => throw(Not_found) | Some(n) /* Node(l, v, d, r, _) */ => let v = n.key let c = Belt_Id.getCmpInternal(cmp)(x, v) diff --git a/runtime/Belt_internalMapInt.res b/runtime/Belt_internalMapInt.res index f60cbd9b24..f51cef9417 100644 --- a/runtime/Belt_internalMapInt.res +++ b/runtime/Belt_internalMapInt.res @@ -65,7 +65,7 @@ let rec getUndefined = (n, x: key) => let rec getExn = (n, x: key) => switch n { - | None => raise(Not_found) + | None => throw(Not_found) | Some(n) => let v = n.N.key if x == v { diff --git a/runtime/Belt_internalMapString.res b/runtime/Belt_internalMapString.res index 4ac6c00535..1fd5f024e2 100644 --- a/runtime/Belt_internalMapString.res +++ b/runtime/Belt_internalMapString.res @@ -65,7 +65,7 @@ let rec getUndefined = (n, x: key) => let rec getExn = (n, x: key) => switch n { - | None => raise(Not_found) + | None => throw(Not_found) | Some(n) => let v = n.N.key if x == v { diff --git a/runtime/Belt_internalSetInt.res b/runtime/Belt_internalSetInt.res index 676e955778..6831794d34 100644 --- a/runtime/Belt_internalSetInt.res +++ b/runtime/Belt_internalSetInt.res @@ -106,7 +106,7 @@ let rec getUndefined = (n: t, x: value) => let rec getExn = (n: t, x: value) => switch n { - | None => raise(Not_found) + | None => throw(Not_found) | Some(t) => let v = t.value if x == v { diff --git a/runtime/Belt_internalSetString.res b/runtime/Belt_internalSetString.res index ef38792cc8..3983818d48 100644 --- a/runtime/Belt_internalSetString.res +++ b/runtime/Belt_internalSetString.res @@ -106,7 +106,7 @@ let rec getUndefined = (n: t, x: value) => let rec getExn = (n: t, x: value) => switch n { - | None => raise(Not_found) + | None => throw(Not_found) | Some(t) => let v = t.value if x == v { diff --git a/runtime/Pervasives.res b/runtime/Pervasives.res index 555a503808..c029346f79 100644 --- a/runtime/Pervasives.res +++ b/runtime/Pervasives.res @@ -1,28 +1,39 @@ -/** - Since [others] depend on this file, its public mli files **should not - export types** introduced here, otherwise it would cause - conflicts here. - - If the type exported here is also exported in modules from others, - you will get a type not equivalent. -*/ @deprecated("Do not use. This will be removed in v13") external /* Internal */ __unsafe_cast: 'a => 'b = "%identity" /* Exceptions */ - +@deprecated( + "`raise` has been renamed to `throw` to align with JavaScript vocabulary. Please use `throw` instead" +) external raise: exn => 'a = "%raise" @deprecated("Use custom exception instead") -let failwith = s => raise(Failure(s)) +let failwith = s => throw(Failure(s)) @deprecated("Use custom exception instead") -let invalid_arg = s => raise(Invalid_argument(s)) +let invalid_arg = s => throw(Invalid_argument(s)) @deprecated("Use custom exception instead") exception Exit +/** +Raises the given exception, terminating execution unless caught by a surrounding try/catch block. + +## Examples + +```rescript +let error = Error.make("Everything is upside down.") + +if 5 > 10 { + throw(error) +} else { + Console.log("Phew, sanity still rules.") +} +``` +*/ +external throw: Stdlib_Error.t => 'a = "%raise" + /* Composition operators */ external \"|>": ('a, 'a => 'b) => 'b = "%revapply" diff --git a/runtime/Pervasives_mini.res b/runtime/Pervasives_mini.res index 483420adb1..24074801a5 100644 --- a/runtime/Pervasives_mini.res +++ b/runtime/Pervasives_mini.res @@ -1,5 +1,5 @@ /* Exceptions */ -external raise: exn => 'a = "%raise" +external throw: exn => 'a = "%raise" /* Debugging */ diff --git a/runtime/Primitive_array.res b/runtime/Primitive_array.res index b521bd1e4c..6a7d994d4d 100644 --- a/runtime/Primitive_array.res +++ b/runtime/Primitive_array.res @@ -2,14 +2,14 @@ let length = Primitive_array_extern.length let get = (xs, index) => if index < 0 || index >= length(xs) { - raise(Invalid_argument("index out of bounds")) + throw(Invalid_argument("index out of bounds")) } else { xs->Primitive_array_extern.getUnsafe(index) } let set = (xs, index, newval) => if index < 0 || index >= length(xs) { - raise(Invalid_argument("index out of bounds")) + throw(Invalid_argument("index out of bounds")) } else { xs->Primitive_array_extern.setUnsafe(index, newval) } diff --git a/runtime/Primitive_bigint.res b/runtime/Primitive_bigint.res index cb576a9357..8f34ecb4c2 100644 --- a/runtime/Primitive_bigint.res +++ b/runtime/Primitive_bigint.res @@ -25,7 +25,7 @@ external div: (bigint, bigint) => bigint = "%divbigint" let div = (x: bigint, y: bigint) => if y == 0n { - raise(Division_by_zero) + throw(Division_by_zero) } else { div(x, y) } @@ -34,7 +34,7 @@ external mod_: (bigint, bigint) => bigint = "%modbigint" let mod_ = (x: bigint, y: bigint) => if y == 0n { - raise(Division_by_zero) + throw(Division_by_zero) } else { mod_(x, y) } diff --git a/runtime/Primitive_int.res b/runtime/Primitive_int.res index 25afa896ee..ce5c4abe0b 100644 --- a/runtime/Primitive_int.res +++ b/runtime/Primitive_int.res @@ -23,14 +23,14 @@ let max = (x: int, y: int): int => let div = (x: int, y: int) => if y == 0 { - raise(Division_by_zero) + throw(Division_by_zero) } else { Primitive_int_extern.div(x, y) } let mod_ = (x: int, y: int) => if y == 0 { - raise(Division_by_zero) + throw(Division_by_zero) } else { Primitive_int_extern.mod_(x, y) } diff --git a/runtime/Primitive_lazy.res b/runtime/Primitive_lazy.res index 9fb7a4f643..f77595bb1f 100644 --- a/runtime/Primitive_lazy.res +++ b/runtime/Primitive_lazy.res @@ -50,7 +50,7 @@ exception Undefined } ) -%%private(let raise_undefined = () => raise(Undefined)) +%%private(let raise_undefined = () => throw(Undefined)) /* Assume [blk] is a block with tag lazy */ %%private( @@ -59,8 +59,8 @@ exception Undefined blk.value = fnToVal(raise_undefined) try forward_with_closure(blk, closure) catch { | e => - blk.value = fnToVal(() => raise(e)) - raise(e) + blk.value = fnToVal(() => throw(e)) + throw(e) } } ) diff --git a/runtime/Primitive_module.res b/runtime/Primitive_module.res index c71f1c6ab8..7a6b7ef57c 100644 --- a/runtime/Primitive_module.res +++ b/runtime/Primitive_module.res @@ -19,7 +19,7 @@ module type Empty = {} in the lambda layer */ let init = (loc: (string, int, int), shape: shape) => { - let undef_module = _ => raise(Undefined_recursive_module(loc)) + let undef_module = _ => throw(Undefined_recursive_module(loc)) let rec loop = (shape: shape, struct_: Obj.t, idx) => switch shape { | Function => Obj.setField(struct_, idx, Obj.magic(undef_module)) diff --git a/runtime/Primitive_object.res b/runtime/Primitive_object.res index 62fdfa743b..cb976d3f2b 100644 --- a/runtime/Primitive_object.res +++ b/runtime/Primitive_object.res @@ -94,7 +94,7 @@ let rec compare = (a: t, b: t): int => | ("boolean", "boolean") => Pervasives.compare((magic(a): bool), magic(b)) | ("boolean", _) => 1 | (_, "boolean") => -1 - | ("function", "function") => raise(Invalid_argument("compare: functional value")) + | ("function", "function") => throw(Invalid_argument("compare: functional value")) | ("function", _) => 1 | (_, "function") => -1 | ("bigint", "bigint") @@ -261,7 +261,7 @@ let rec equal = (a: t, b: t): bool => } else { let b_type = Js.typeof(b) if a_type == "function" || b_type == "function" { - raise(Invalid_argument("equal: functional value")) + throw(Invalid_argument("equal: functional value")) } /* first, check using reference equality */ else if ( /* a_type = "object" || "symbol" */ diff --git a/runtime/Primitive_string.res b/runtime/Primitive_string.res index fe6c2b127b..4a07228a02 100644 --- a/runtime/Primitive_string.res +++ b/runtime/Primitive_string.res @@ -23,7 +23,7 @@ let max = (x: string, y: string): string => let getChar = (s, i) => if i >= Primitive_string_extern.length(s) || i < 0 { - raise(Invalid_argument("index out of bounds")) + throw(Invalid_argument("index out of bounds")) } else { Primitive_string_extern.getChar(s, i) } diff --git a/runtime/Primitive_util.res b/runtime/Primitive_util.res index 1b4cb015b9..f2a5bd7e5c 100644 --- a/runtime/Primitive_util.res +++ b/runtime/Primitive_util.res @@ -2,7 +2,7 @@ module Js = Primitive_js_extern let raiseWhenNotFound = x => if Js.testAny(x) { - raise(Not_found) + throw(Not_found) } else { x } diff --git a/runtime/Stdlib_Bool.res b/runtime/Stdlib_Bool.res index 0d4371ddfc..27f04f31df 100644 --- a/runtime/Stdlib_Bool.res +++ b/runtime/Stdlib_Bool.res @@ -19,7 +19,7 @@ let fromStringExn = param => switch param { | "true" => true | "false" => false - | _ => raise(Invalid_argument(`Bool.fromStringExn: value is neither "true" nor "false"`)) + | _ => throw(Invalid_argument(`Bool.fromStringExn: value is neither "true" nor "false"`)) } external compare: (bool, bool) => Stdlib_Ordering.t = "%compare" diff --git a/runtime/Stdlib_Char.res b/runtime/Stdlib_Char.res index 7e28ed66ae..f846c78a0f 100644 --- a/runtime/Stdlib_Char.res +++ b/runtime/Stdlib_Char.res @@ -10,7 +10,7 @@ external fromIntUnsafe: int => t = "%identity" let fromIntExn = n => if n < 0 || n > 255 { - raise(Invalid_argument("`Char.fromIntExn` expects an integer between 0 and 255")) + throw(Invalid_argument("`Char.fromIntExn` expects an integer between 0 and 255")) } else { fromIntUnsafe(n) } diff --git a/runtime/Stdlib_Error.res b/runtime/Stdlib_Error.res index 0934fe58f1..f2035d178d 100644 --- a/runtime/Stdlib_Error.res +++ b/runtime/Stdlib_Error.res @@ -40,6 +40,8 @@ module URIError = { external raise: t => 'a = "%raise" +external throw: t => 'a = "%raise" + let panic = msg => make(`Panic! ${msg}`)->raise external ignore: t => unit = "%ignore" diff --git a/runtime/Stdlib_Error.resi b/runtime/Stdlib_Error.resi index e75c91195e..45cbed665d 100644 --- a/runtime/Stdlib_Error.resi +++ b/runtime/Stdlib_Error.resi @@ -155,8 +155,28 @@ if 5 > 10 { } ``` */ +@deprecated( + "`raise` has been renamed to `throw` to align with JavaScript vocabulary. Please use `throw` instead" +) external raise: t => 'a = "%raise" +/** +Raises the given exception, terminating execution unless caught by a surrounding try/catch block. + +## Examples + +```rescript +let error = Error.make("Everything is upside down.") + +if 5 > 10 { + Error.throw(error) +} else { + Console.log("Phew, sanity still rules.") +} +``` +*/ +external throw: t => 'a = "%raise" + /** Raises a panic exception with the given message. diff --git a/runtime/Stdlib_Exn.res b/runtime/Stdlib_Exn.res index c91fe4845b..20e6753680 100644 --- a/runtime/Stdlib_Exn.res +++ b/runtime/Stdlib_Exn.res @@ -43,38 +43,38 @@ type error external anyToExnInternal: 'a => exn = "%wrap_exn" -let raiseError = str => raise((Obj.magic((makeError(str): error)): exn)) +let raiseError = str => throw((Obj.magic((makeError(str): error)): exn)) type eval_error @new external makeEvalError: string => eval_error = "EvalError" -let raiseEvalError = str => raise((Obj.magic((makeEvalError(str): eval_error)): exn)) +let raiseEvalError = str => throw((Obj.magic((makeEvalError(str): eval_error)): exn)) type range_error @new external makeRangeError: string => range_error = "RangeError" -let raiseRangeError = str => raise((Obj.magic((makeRangeError(str): range_error)): exn)) +let raiseRangeError = str => throw((Obj.magic((makeRangeError(str): range_error)): exn)) type reference_error @new external makeReferenceError: string => reference_error = "ReferenceError" -let raiseReferenceError = str => raise(Obj.magic(makeReferenceError(str))) +let raiseReferenceError = str => throw(Obj.magic(makeReferenceError(str))) type syntax_error @new external makeSyntaxError: string => syntax_error = "SyntaxError" -let raiseSyntaxError = str => raise(Obj.magic(makeSyntaxError(str))) +let raiseSyntaxError = str => throw(Obj.magic(makeSyntaxError(str))) type type_error @new external makeTypeError: string => type_error = "TypeError" -let raiseTypeError = str => raise(Obj.magic(makeTypeError(str))) +let raiseTypeError = str => throw(Obj.magic(makeTypeError(str))) type uri_error @new external makeURIError: string => uri_error = "URIError" -let raiseUriError = str => raise(Obj.magic(makeURIError(str))) +let raiseUriError = str => throw(Obj.magic(makeURIError(str))) /* TODO add predicate to tell which error is which " */ diff --git a/runtime/Stdlib_Int.res b/runtime/Stdlib_Int.res index 0959a8b25f..4e4ebbecd4 100644 --- a/runtime/Stdlib_Int.res +++ b/runtime/Stdlib_Int.res @@ -64,7 +64,7 @@ let range = (start, end, ~options: rangeOptions={}) => { let step = switch options.step { | None => isInverted ? -1 : 1 | Some(0) if start !== end => - Stdlib_Error.raise(Stdlib_Error.RangeError.make("Incorrect range arguments")) + Stdlib_Error.throw(Stdlib_Error.RangeError.make("Incorrect range arguments")) | Some(n) => n } diff --git a/runtime/Stdlib_List.res b/runtime/Stdlib_List.res index c76ba2b577..c6269c30fd 100644 --- a/runtime/Stdlib_List.res +++ b/runtime/Stdlib_List.res @@ -108,7 +108,7 @@ let head = x => let headExn = x => switch x { - | list{} => raise(Not_found) + | list{} => throw(Not_found) | list{x, ..._} => x } @@ -120,7 +120,7 @@ let tail = x => let tailExn = x => switch x { - | list{} => raise(Not_found) + | list{} => throw(Not_found) | list{_, ...t} => t } @@ -146,7 +146,7 @@ let rec nthAuxAssert = (x, n) => } else { nthAuxAssert(t, n - 1) } - | _ => raise(Not_found) + | _ => throw(Not_found) } let get = (x, n) => @@ -158,7 +158,7 @@ let get = (x, n) => let getExn = (x, n) => if n < 0 { - raise(Not_found) + throw(Not_found) } else { nthAuxAssert(x, n) } diff --git a/runtime/Stdlib_Null.res b/runtime/Stdlib_Null.res index d6ce47b70c..37aa6947fc 100644 --- a/runtime/Stdlib_Null.res +++ b/runtime/Stdlib_Null.res @@ -32,7 +32,7 @@ let getWithDefault = getOr let getExn: t<'a> => 'a = value => switch value->toOption { | Some(x) => x - | None => raise(Invalid_argument("Null.getExn: value is null")) + | None => throw(Invalid_argument("Null.getExn: value is null")) } external getUnsafe: t<'a> => 'a = "%identity" diff --git a/runtime/Stdlib_Nullable.res b/runtime/Stdlib_Nullable.res index 56fd3ce2c9..25b854b93a 100644 --- a/runtime/Stdlib_Nullable.res +++ b/runtime/Stdlib_Nullable.res @@ -35,7 +35,7 @@ let getWithDefault = getOr let getExn: t<'a> => 'a = value => switch value->toOption { | Some(x) => x - | None => raise(Invalid_argument("Nullable.getExn: value is null or undefined")) + | None => throw(Invalid_argument("Nullable.getExn: value is null or undefined")) } external getUnsafe: t<'a> => 'a = "%identity" diff --git a/runtime/Stdlib_Result.res b/runtime/Stdlib_Result.res index b06f8c0a2a..b2b7f10219 100644 --- a/runtime/Stdlib_Result.res +++ b/runtime/Stdlib_Result.res @@ -26,7 +26,7 @@ type t<'res, 'err> = result<'res, 'err> = Ok('res) | Error('err) let getExn = x => switch x { | Ok(x) => x - | Error(_) => raise(Not_found) + | Error(_) => throw(Not_found) } let mapOr = (opt, default, f) =>