Skip to content

Move result type from Belt to built-in. #6450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions jscomp/ml/predef.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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))
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 (
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions jscomp/ml/predef.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion jscomp/others/belt_Result.res
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
21 changes: 1 addition & 20 deletions jscomp/others/belt_Result.resi
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, responseError>

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
Expand Down
6 changes: 0 additions & 6 deletions jscomp/stdlib-406/pervasives.res
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,6 @@ external \":=": (ref<'a>, 'a) => unit = "%bs_ref_setfield0"
external incr: ref<int> => unit = "%incr"
external decr: ref<int> => 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"

Expand Down
7 changes: 0 additions & 7 deletions jscomp/stdlib-406/pervasives.resi
Original file line number Diff line number Diff line change
Expand Up @@ -735,13 +735,6 @@ external incr: ref<int> => unit = "%incr"
Equivalent to [fun r -> r := pred !r]. */
external decr: ref<int> => 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
Expand Down
6 changes: 0 additions & 6 deletions jscomp/stdlib-406/pervasivesU.res
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,6 @@ external \":=": (ref<'a>, 'a) => unit = "%bs_ref_setfield0"
external incr: ref<int> => unit = "%incr"
external decr: ref<int> => 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"

Expand Down
7 changes: 0 additions & 7 deletions jscomp/stdlib-406/pervasivesU.resi
Original file line number Diff line number Diff line change
Expand Up @@ -738,13 +738,6 @@ external incr: ref<int> => unit = "%incr"
Equivalent to [fun r -> r := pred !r]. */
external decr: ref<int> => 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
Expand Down
Loading