Skip to content

Commit 02f84a1

Browse files
committed
Move result type from Belt to built-in.
1 parent c8cfd63 commit 02f84a1

File tree

9 files changed

+164
-195
lines changed

9 files changed

+164
-195
lines changed

jscomp/ml/predef.ml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ and ident_exn = ident_create "exn"
3939
and ident_array = ident_create "array"
4040
and ident_list = ident_create "list"
4141
and ident_option = ident_create "option"
42+
and ident_result = ident_create "result"
4243

4344
and ident_int64 = ident_create "int64"
4445
and ident_lazy_t = ident_create "lazy_t"
@@ -80,6 +81,7 @@ and path_exn = Pident ident_exn
8081
and path_array = Pident ident_array
8182
and path_list = Pident ident_list
8283
and path_option = Pident ident_option
84+
and path_result = Pident ident_result
8385

8486

8587
and path_int64 = Pident ident_int64
@@ -102,7 +104,7 @@ and type_exn = newgenty (Tconstr(path_exn, [], ref Mnil))
102104
and type_array t = newgenty (Tconstr(path_array, [t], ref Mnil))
103105
and type_list t = newgenty (Tconstr(path_list, [t], ref Mnil))
104106
and type_option t = newgenty (Tconstr(path_option, [t], ref Mnil))
105-
107+
and type_result t1 t2 = newgenty (Tconstr(path_result, [t1; t2], ref Mnil))
106108

107109
and type_int64 = newgenty (Tconstr(path_int64, [], ref Mnil))
108110
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"
117119

118120
and ident_invalid_argument = ident_create_predef_exn "Invalid_argument"
119121
and ident_failure = ident_create_predef_exn "Failure"
122+
and ident_ok = ident_create_predef_exn "Ok"
123+
and ident_error = ident_create_predef_exn "Error"
120124

121125
and ident_js_error = ident_create_predef_exn "JsError"
122126
and ident_not_found = ident_create_predef_exn "Not_found"
@@ -213,6 +217,15 @@ let common_initial_env add_type add_extension empty_env =
213217
type_arity = 1;
214218
type_kind = Type_variant([cstr ident_none []; cstr ident_some [tvar]]);
215219
type_variance = [Variance.covariant]}
220+
and decl_result =
221+
let tvar1, tvar2 = newgenvar(), newgenvar() in
222+
{decl_abstr with
223+
type_params = [tvar1; tvar2];
224+
type_arity = 2;
225+
type_kind =
226+
Type_variant([cstr ident_ok [tvar1];
227+
cstr ident_error [tvar2]]);
228+
type_variance = [Variance.covariant; Variance.covariant]}
216229
and decl_uncurried =
217230
let tvar1, tvar2 = newgenvar(), newgenvar() in
218231
{decl_abstr with
@@ -278,6 +291,7 @@ let common_initial_env add_type add_extension empty_env =
278291

279292
add_type ident_lazy_t decl_lazy_t (
280293
add_type ident_option decl_option (
294+
add_type ident_result decl_result (
281295
add_type ident_list decl_list (
282296
add_type ident_array decl_array (
283297
add_type ident_exn decl_exn (
@@ -291,7 +305,7 @@ let common_initial_env add_type add_extension empty_env =
291305
add_type ident_extension_constructor decl_abstr (
292306
add_type ident_floatarray decl_abstr (
293307
add_type ident_promise decl_promise (
294-
empty_env)))))))))))))))))))))))))
308+
empty_env))))))))))))))))))))))))))
295309

296310
let build_initial_env add_type add_exception empty_env =
297311
let common = common_initial_env add_type add_exception empty_env in

jscomp/ml/predef.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ val type_exn: type_expr
2828
val type_array: type_expr -> type_expr
2929
val type_list: type_expr -> type_expr
3030
val type_option: type_expr -> type_expr
31-
31+
val type_result: type_expr -> type_expr -> type_expr
3232

3333
val type_int64: type_expr
3434
val type_lazy_t: type_expr -> type_expr
@@ -46,7 +46,7 @@ val path_exn: Path.t
4646
val path_array: Path.t
4747
val path_list: Path.t
4848
val path_option: Path.t
49-
49+
val path_result: Path.t
5050

5151
val path_int64: Path.t
5252
val path_lazy_t: Path.t

jscomp/others/belt_Result.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
2424

25-
type t<'a, 'b> = Ok('a) | Error('b)
25+
type t<'a, 'b> = result<'a, 'b>
2626

2727
let getExn = x =>
2828
switch x {

jscomp/others/belt_Result.resi

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,7 @@
2929
This module gives you useful utilities to create and combine `Result` data.
3030
*/
3131

32-
type t<'a, 'b> =
33-
| Ok('a)
34-
| /**
35-
The type `Result.t(result, err)` describes a variant of two states:
36-
`Ok(someResult)` represents a successful operation, whereby
37-
``Error(someError)` signals an erronous operation.
38-
39-
In this concrete example, we are defining our own `Result` type to reflect an HTTP like
40-
query operation:
41-
42-
```res example
43-
type responseError = NotAvailable | NotFound
44-
type queryResult = t<string, responseError>
45-
46-
let failQueryUser = (username: string): queryResult => {
47-
Error(NotAvailable)
48-
}
49-
```
50-
*/
51-
Error('b)
32+
type t<'a, 'b> = result<'a, 'b>
5233

5334
/**
5435
`getExn(res)`: when `res` is `Ok(n)`, returns `n` when `res` is `Error(m)`, raise an exception

jscomp/stdlib-406/pervasives.res

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,6 @@ external \":=": (ref<'a>, 'a) => unit = "%bs_ref_setfield0"
213213
external incr: ref<int> => unit = "%incr"
214214
external decr: ref<int> => unit = "%decr"
215215

216-
/* Result type */
217-
218-
type result<'a, 'b> = Belt.Result.t<'a, 'b> =
219-
| Ok('a)
220-
| Error('b)
221-
222216
/* String conversion functions */
223217
external format_float: (string, float) => string = "?format_float"
224218

jscomp/stdlib-406/pervasives.resi

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -735,13 +735,6 @@ external incr: ref<int> => unit = "%incr"
735735
Equivalent to [fun r -> r := pred !r]. */
736736
external decr: ref<int> => unit = "%decr"
737737

738-
/* {1 Result type} */
739-
740-
/** @since 4.03.0 */
741-
type result<'a, 'b> = Belt.Result.t<'a, 'b> =
742-
| Ok('a)
743-
| Error('b)
744-
745738
/* {1 Program termination} */
746739

747740
/** Terminate the process, returning the given status code

jscomp/stdlib-406/pervasivesU.res

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,6 @@ external \":=": (ref<'a>, 'a) => unit = "%bs_ref_setfield0"
214214
external incr: ref<int> => unit = "%incr"
215215
external decr: ref<int> => unit = "%decr"
216216

217-
/* Result type */
218-
219-
type result<'a, 'b> = Belt.Result.t<'a, 'b> =
220-
| Ok('a)
221-
| Error('b)
222-
223217
/* String conversion functions */
224218
external format_float: (string, float) => string = "?format_float"
225219

jscomp/stdlib-406/pervasivesU.resi

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -738,13 +738,6 @@ external incr: ref<int> => unit = "%incr"
738738
Equivalent to [fun r -> r := pred !r]. */
739739
external decr: ref<int> => unit = "%decr"
740740

741-
/*** {1 Result type} */
742-
743-
/** @since 4.03.0 */
744-
type result<'a, 'b> = Belt.Result.t<'a, 'b> =
745-
| Ok('a)
746-
| Error('b)
747-
748741
/*** {1 Program termination} */
749742

750743
/** Terminate the process, returning the given status code

0 commit comments

Comments
 (0)