diff --git a/jscomp/ml/predef.ml b/jscomp/ml/predef.ml index c9516c19de..fe5749d7dc 100644 --- a/jscomp/ml/predef.ml +++ b/jscomp/ml/predef.ml @@ -39,6 +39,7 @@ and ident_exn = ident_create "exn" and ident_array = ident_create "array" and ident_list = ident_create "list" and ident_option = ident_create "option" +and ident_result = ident_create "result" and ident_int64 = ident_create "int64" and ident_lazy_t = ident_create "lazy_t" @@ -80,6 +81,7 @@ and path_exn = Pident ident_exn and path_array = Pident ident_array and path_list = Pident ident_list and path_option = Pident ident_option +and path_result = Pident ident_result and path_int64 = Pident ident_int64 @@ -102,7 +104,7 @@ and type_exn = newgenty (Tconstr(path_exn, [], ref Mnil)) and type_array t = newgenty (Tconstr(path_array, [t], ref Mnil)) and type_list t = newgenty (Tconstr(path_list, [t], ref Mnil)) and type_option t = newgenty (Tconstr(path_option, [t], ref Mnil)) - +and type_result t1 t2 = newgenty (Tconstr(path_result, [t1; t2], ref Mnil)) and type_int64 = newgenty (Tconstr(path_int64, [], ref Mnil)) and type_lazy_t t = newgenty (Tconstr(path_lazy_t, [t], ref Mnil)) @@ -117,6 +119,8 @@ let ident_match_failure = ident_create_predef_exn "Match_failure" and ident_invalid_argument = ident_create_predef_exn "Invalid_argument" and ident_failure = ident_create_predef_exn "Failure" +and ident_ok = ident_create_predef_exn "Ok" +and ident_error = ident_create_predef_exn "Error" and ident_js_error = ident_create_predef_exn "JsError" and ident_not_found = ident_create_predef_exn "Not_found" @@ -213,6 +217,15 @@ let common_initial_env add_type add_extension empty_env = type_arity = 1; type_kind = Type_variant([cstr ident_none []; cstr ident_some [tvar]]); type_variance = [Variance.covariant]} + and decl_result = + let tvar1, tvar2 = newgenvar(), newgenvar() in + {decl_abstr with + type_params = [tvar1; tvar2]; + type_arity = 2; + type_kind = + Type_variant([cstr ident_ok [tvar1]; + cstr ident_error [tvar2]]); + type_variance = [Variance.covariant; Variance.covariant]} and decl_uncurried = let tvar1, tvar2 = newgenvar(), newgenvar() in {decl_abstr with @@ -278,6 +291,7 @@ let common_initial_env add_type add_extension empty_env = add_type ident_lazy_t decl_lazy_t ( add_type ident_option decl_option ( + add_type ident_result decl_result ( add_type ident_list decl_list ( add_type ident_array decl_array ( add_type ident_exn decl_exn ( @@ -291,7 +305,7 @@ let common_initial_env add_type add_extension empty_env = add_type ident_extension_constructor decl_abstr ( add_type ident_floatarray decl_abstr ( add_type ident_promise decl_promise ( - empty_env))))))))))))))))))))))))) + empty_env)))))))))))))))))))))))))) let build_initial_env add_type add_exception empty_env = let common = common_initial_env add_type add_exception empty_env in diff --git a/jscomp/ml/predef.mli b/jscomp/ml/predef.mli index d6f82144e2..a998aa6807 100644 --- a/jscomp/ml/predef.mli +++ b/jscomp/ml/predef.mli @@ -28,7 +28,7 @@ val type_exn: type_expr val type_array: type_expr -> type_expr val type_list: type_expr -> type_expr val type_option: type_expr -> type_expr - +val type_result: type_expr -> type_expr -> type_expr val type_int64: type_expr val type_lazy_t: type_expr -> type_expr @@ -46,7 +46,7 @@ val path_exn: Path.t val path_array: Path.t val path_list: Path.t val path_option: Path.t - +val path_result: Path.t val path_int64: Path.t val path_lazy_t: Path.t diff --git a/jscomp/others/belt_Result.res b/jscomp/others/belt_Result.res index 08d381c815..607ad01fd6 100644 --- a/jscomp/others/belt_Result.res +++ b/jscomp/others/belt_Result.res @@ -22,7 +22,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -type t<'a, 'b> = Ok('a) | Error('b) +type t<'a, 'b> = result<'a, 'b> let getExn = x => switch x { diff --git a/jscomp/others/belt_Result.resi b/jscomp/others/belt_Result.resi index 1d17f52475..c88d5c736f 100644 --- a/jscomp/others/belt_Result.resi +++ b/jscomp/others/belt_Result.resi @@ -29,26 +29,7 @@ This module gives you useful utilities to create and combine `Result` data. */ -type t<'a, 'b> = - | Ok('a) - | /** - The type `Result.t(result, err)` describes a variant of two states: - `Ok(someResult)` represents a successful operation, whereby - ``Error(someError)` signals an erronous operation. - - In this concrete example, we are defining our own `Result` type to reflect an HTTP like - query operation: - - ```res example - type responseError = NotAvailable | NotFound - type queryResult = t - - let failQueryUser = (username: string): queryResult => { - Error(NotAvailable) - } -``` -*/ - Error('b) +type t<'a, 'b> = result<'a, 'b> /** `getExn(res)`: when `res` is `Ok(n)`, returns `n` when `res` is `Error(m)`, raise an exception diff --git a/jscomp/stdlib-406/pervasives.res b/jscomp/stdlib-406/pervasives.res index 4ccc65b7ab..632d0b7228 100644 --- a/jscomp/stdlib-406/pervasives.res +++ b/jscomp/stdlib-406/pervasives.res @@ -213,12 +213,6 @@ external \":=": (ref<'a>, 'a) => unit = "%bs_ref_setfield0" external incr: ref => unit = "%incr" external decr: ref => unit = "%decr" -/* Result type */ - -type result<'a, 'b> = Belt.Result.t<'a, 'b> = - | Ok('a) - | Error('b) - /* String conversion functions */ external format_float: (string, float) => string = "?format_float" diff --git a/jscomp/stdlib-406/pervasives.resi b/jscomp/stdlib-406/pervasives.resi index 46b0eb5e68..43407ced92 100644 --- a/jscomp/stdlib-406/pervasives.resi +++ b/jscomp/stdlib-406/pervasives.resi @@ -735,13 +735,6 @@ external incr: ref => unit = "%incr" Equivalent to [fun r -> r := pred !r]. */ external decr: ref => unit = "%decr" -/* {1 Result type} */ - -/** @since 4.03.0 */ -type result<'a, 'b> = Belt.Result.t<'a, 'b> = - | Ok('a) - | Error('b) - /* {1 Program termination} */ /** Terminate the process, returning the given status code diff --git a/jscomp/stdlib-406/pervasivesU.res b/jscomp/stdlib-406/pervasivesU.res index 237d0dabc0..c044504bb7 100644 --- a/jscomp/stdlib-406/pervasivesU.res +++ b/jscomp/stdlib-406/pervasivesU.res @@ -214,12 +214,6 @@ external \":=": (ref<'a>, 'a) => unit = "%bs_ref_setfield0" external incr: ref => unit = "%incr" external decr: ref => unit = "%decr" -/* Result type */ - -type result<'a, 'b> = Belt.Result.t<'a, 'b> = - | Ok('a) - | Error('b) - /* String conversion functions */ external format_float: (string, float) => string = "?format_float" diff --git a/jscomp/stdlib-406/pervasivesU.resi b/jscomp/stdlib-406/pervasivesU.resi index 2526d1dee7..4b0f05928b 100644 --- a/jscomp/stdlib-406/pervasivesU.resi +++ b/jscomp/stdlib-406/pervasivesU.resi @@ -738,13 +738,6 @@ external incr: ref => unit = "%incr" Equivalent to [fun r -> r := pred !r]. */ external decr: ref => unit = "%decr" -/*** {1 Result type} */ - -/** @since 4.03.0 */ -type result<'a, 'b> = Belt.Result.t<'a, 'b> = - | Ok('a) - | Error('b) - /*** {1 Program termination} */ /** Terminate the process, returning the given status code diff --git a/jscomp/test/ocaml_re_test.js b/jscomp/test/ocaml_re_test.js index 105e000cd0..d3ca5ba7f6 100644 --- a/jscomp/test/ocaml_re_test.js +++ b/jscomp/test/ocaml_re_test.js @@ -3412,69 +3412,6 @@ function parse(multiline, dollar_endonly, dotall, ungreedy, s) { }; } }; - var piece = function (param) { - var r = atom(undefined); - if (accept(/* '*' */42)) { - return greedy_mod(repn(r, 0, undefined)); - } - if (accept(/* '+' */43)) { - return greedy_mod(repn(r, 1, undefined)); - } - if (accept(/* '?' */63)) { - return greedy_mod(repn(r, 0, 1)); - } - if (!accept(/* '{' */123)) { - return r; - } - var i$1 = integer(undefined); - if (i$1 !== undefined) { - var j = accept(/* ',' */44) ? integer(undefined) : i$1; - if (!accept(/* '}' */125)) { - throw { - RE_EXN_ID: Parse_error, - Error: new Error() - }; - } - if (j !== undefined && j < i$1) { - throw { - RE_EXN_ID: Parse_error, - Error: new Error() - }; - } - return greedy_mod(repn(r, i$1, j)); - } - i.contents = i.contents - 1 | 0; - return r; - }; - var branch$p = function (_left) { - while(true) { - var left = _left; - if (i.contents === l || test(/* '|' */124) || test(/* ')' */41)) { - return seq$2(List.rev(left)); - } - _left = { - hd: piece(undefined), - tl: left - }; - continue ; - }; - }; - var regexp$p = function (_left) { - while(true) { - var left = _left; - if (!accept(/* '|' */124)) { - return left; - } - _left = alt$1({ - hd: left, - tl: { - hd: branch$p(/* [] */0), - tl: /* [] */0 - } - }); - continue ; - }; - }; var atom = function (param) { if (accept(/* '.' */46)) { if (dotall) { @@ -3784,87 +3721,6 @@ function parse(multiline, dollar_endonly, dotall, ungreedy, s) { }; } }; - var bracket = function (_s) { - while(true) { - var s = _s; - if (s !== /* [] */0 && accept(/* ']' */93)) { - return s; - } - var match = $$char(undefined); - if (match.NAME === "Char") { - var c = match.VAL; - if (accept(/* '-' */45)) { - if (accept(/* ']' */93)) { - return { - hd: { - TAG: "Set", - _0: single(c) - }, - tl: { - hd: { - TAG: "Set", - _0: { - hd: [ - /* '-' */45, - /* '-' */45 - ], - tl: /* [] */0 - } - }, - tl: s - } - }; - } - var match$1 = $$char(undefined); - if (match$1.NAME !== "Char") { - return { - hd: { - TAG: "Set", - _0: single(c) - }, - tl: { - hd: { - TAG: "Set", - _0: { - hd: [ - /* '-' */45, - /* '-' */45 - ], - tl: /* [] */0 - } - }, - tl: { - hd: match$1.VAL, - tl: s - } - } - }; - } - _s = { - hd: { - TAG: "Set", - _0: seq(c, match$1.VAL) - }, - tl: s - }; - continue ; - } - _s = { - hd: { - TAG: "Set", - _0: single(c) - }, - tl: s - }; - continue ; - } - _s = { - hd: match.VAL, - tl: s - }; - continue ; - }; - }; var $$char = function (param) { if (i.contents === l) { throw { @@ -4160,6 +4016,150 @@ function parse(multiline, dollar_endonly, dotall, ungreedy, s) { }; } }; + var bracket = function (_s) { + while(true) { + var s = _s; + if (s !== /* [] */0 && accept(/* ']' */93)) { + return s; + } + var match = $$char(undefined); + if (match.NAME === "Char") { + var c = match.VAL; + if (accept(/* '-' */45)) { + if (accept(/* ']' */93)) { + return { + hd: { + TAG: "Set", + _0: single(c) + }, + tl: { + hd: { + TAG: "Set", + _0: { + hd: [ + /* '-' */45, + /* '-' */45 + ], + tl: /* [] */0 + } + }, + tl: s + } + }; + } + var match$1 = $$char(undefined); + if (match$1.NAME !== "Char") { + return { + hd: { + TAG: "Set", + _0: single(c) + }, + tl: { + hd: { + TAG: "Set", + _0: { + hd: [ + /* '-' */45, + /* '-' */45 + ], + tl: /* [] */0 + } + }, + tl: { + hd: match$1.VAL, + tl: s + } + } + }; + } + _s = { + hd: { + TAG: "Set", + _0: seq(c, match$1.VAL) + }, + tl: s + }; + continue ; + } + _s = { + hd: { + TAG: "Set", + _0: single(c) + }, + tl: s + }; + continue ; + } + _s = { + hd: match.VAL, + tl: s + }; + continue ; + }; + }; + var piece = function (param) { + var r = atom(undefined); + if (accept(/* '*' */42)) { + return greedy_mod(repn(r, 0, undefined)); + } + if (accept(/* '+' */43)) { + return greedy_mod(repn(r, 1, undefined)); + } + if (accept(/* '?' */63)) { + return greedy_mod(repn(r, 0, 1)); + } + if (!accept(/* '{' */123)) { + return r; + } + var i$1 = integer(undefined); + if (i$1 !== undefined) { + var j = accept(/* ',' */44) ? integer(undefined) : i$1; + if (!accept(/* '}' */125)) { + throw { + RE_EXN_ID: Parse_error, + Error: new Error() + }; + } + if (j !== undefined && j < i$1) { + throw { + RE_EXN_ID: Parse_error, + Error: new Error() + }; + } + return greedy_mod(repn(r, i$1, j)); + } + i.contents = i.contents - 1 | 0; + return r; + }; + var branch$p = function (_left) { + while(true) { + var left = _left; + if (i.contents === l || test(/* '|' */124) || test(/* ')' */41)) { + return seq$2(List.rev(left)); + } + _left = { + hd: piece(undefined), + tl: left + }; + continue ; + }; + }; + var regexp$p = function (_left) { + while(true) { + var left = _left; + if (!accept(/* '|' */124)) { + return left; + } + _left = alt$1({ + hd: left, + tl: { + hd: branch$p(/* [] */0), + tl: /* [] */0 + } + }); + continue ; + }; + }; var res = regexp$p(branch$p(/* [] */0)); if (i.contents !== l) { throw {