Skip to content

Commit a7b1ac4

Browse files
authored
Unboxed variants stdlib types (#6218)
1 parent 44f4fa6 commit a7b1ac4

File tree

12 files changed

+62
-16
lines changed

12 files changed

+62
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
- Add location information to duplicate type definition error messages. https://github.com/rescript-lang/rescript-compiler/pull/6199
2222
- Replace normal module errors with Super_error module, and clean up Super_error. https://github.com/rescript-lang/rescript-compiler/pull/6199
23+
- `Js.Json.t`, `Js.null` and `Js.nullable` are now untagged variants representing their runtime values, instead of abstract types. https://github.com/rescript-lang/rescript-compiler/pull/6218
2324

2425
# 11.0.0-alpha.5
2526

jscomp/others/js.ml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ end
8686

8787
(**/**)
8888

89-
type +'a null
89+
type +'a null =
90+
| Value of 'a
91+
| Null [@as null]
92+
[@@unboxed]
9093
(**
9194
Nullable value of this type can be either null or 'a. This type is equivalent to Js.Null.t.
9295
*)
@@ -96,7 +99,12 @@ type +'a undefined
9699
A value of this type can be either undefined or 'a. This type is equivalent to Js.Undefined.t.
97100
*)
98101

99-
type +'a nullable
102+
type +'a nullable =
103+
| Value of 'a
104+
| Null [@as null]
105+
| Undefined [@as undefined]
106+
[@@unboxed]
107+
100108
(**
101109
A value of this type can be undefined, null or 'a. This type is equivalent to Js.Null_undefined.t.
102110
*)

jscomp/others/js_json.ml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,15 @@
2424

2525
(** Efficient JSON encoding using JavaScript API *)
2626

27-
type t
27+
type t =
28+
| False [@as false]
29+
| True [@as true]
30+
| Null [@as null]
31+
| String of string
32+
| Number of float
33+
| Object of t Js.Dict.t
34+
| Array of t array
35+
[@@unboxed]
2836

2937
type _ kind =
3038
| String : Js_string.t kind

jscomp/others/js_json.mli

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@
3030

3131
(** ## Types *)
3232

33-
type t
33+
type t =
34+
| False [@as false]
35+
| True [@as true]
36+
| Null [@as null]
37+
| String of string
38+
| Number of float
39+
| Object of t Js.Dict.t
40+
| Array of t array
41+
[@@unboxed]
3442
(** The JSON data structure *)
3543

3644
(** Underlying type of a JSON value *)

jscomp/others/js_null.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
(** Provides functionality for dealing with the `'a Js.null` type *)
2626

2727

28-
type + 'a t = 'a Js.null
28+
type + 'a t = 'a Js.null =
29+
| Value of 'a
30+
| Null [@as null]
31+
[@@unboxed]
2932

3033
external to_opt : 'a t -> 'a option = "#null_to_opt"
3134
external toOption : 'a t -> 'a option = "#null_to_opt"

jscomp/others/js_null.mli

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626

2727
(** Provides functionality for dealing with the `Js.null('a)` type *)
2828

29-
type +'a t = 'a Js.null
29+
type +'a t = 'a Js.null =
30+
| Value of 'a
31+
| Null [@as null]
32+
[@@unboxed]
3033
(** Local alias for `Js.null('a)` *)
3134

3235
external return : 'a -> 'a t = "%identity"

jscomp/others/js_null_undefined.ml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@
2424

2525
(** Contains functionality for dealing with values that can be both `null` and `undefined` *)
2626

27-
type + 'a t = 'a Js.nullable
27+
type + 'a t = 'a Js.nullable =
28+
| Value of 'a
29+
| Null [@as null]
30+
| Undefined [@as undefined]
31+
[@@unboxed]
32+
2833
external toOption : 'a t -> 'a option = "#nullable_to_opt"
2934
external to_opt : 'a t -> 'a option = "#nullable_to_opt"
3035
external return : 'a -> 'a t = "%identity"

jscomp/others/js_null_undefined.mli

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424

2525
(** Contains functionality for dealing with values that can be both `null` and `undefined` *)
2626

27-
type +'a t = 'a Js.null_undefined
27+
type +'a t = 'a Js.nullable =
28+
| Value of 'a
29+
| Null [@as null]
30+
| Undefined [@as undefined]
31+
[@@unboxed]
2832
(** Local alias for `Js.null_undefined('a)`. *)
2933

3034
external return : 'a -> 'a t = "%identity"

jscomp/others/release.ninja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ o others/js_float.cmi others/js_float.cmj : cc others/js_float.ml | others/belt_
3232
o others/js_global.cmi others/js_global.cmj : cc others/js_global.ml | others/belt_internals.cmi others/js.cmi $bsc
3333
o others/js_int.cmi others/js_int.cmj : cc others/js_int.ml | others/belt_internals.cmi others/js.cmi $bsc
3434
o others/js_json.cmj : cc_cmi others/js_json.ml | others/belt_internals.cmi others/js.cmi others/js.cmj others/js_array2.cmj others/js_dict.cmj others/js_json.cmi others/js_string.cmj others/js_types.cmj $bsc
35-
o others/js_json.cmi : cc others/js_json.mli | others/belt_internals.cmi others/js.cmi others/js_dict.cmi others/js_null.cmi others/js_string.cmj others/js_types.cmi $bsc
35+
o others/js_json.cmi : cc others/js_json.mli | others/belt_internals.cmi others/js.cmi others/js.cmj others/js_dict.cmi others/js_null.cmi others/js_string.cmj others/js_types.cmi $bsc
3636
o others/js_list.cmj : cc_cmi others/js_list.ml | others/belt_internals.cmi others/js.cmi others/js_array2.cmj others/js_list.cmi others/js_vector.cmj $bsc
3737
o others/js_list.cmi : cc others/js_list.mli | others/belt_internals.cmi others/js.cmi $bsc
3838
o others/js_map.cmi others/js_map.cmj : cc others/js_map.ml | others/belt_internals.cmi others/js.cmi $bsc

jscomp/runtime/js.ml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ end
5757
(**/**)
5858

5959

60-
type + 'a null
60+
type + 'a null =
61+
| Value of 'a
62+
| Null [@as null]
63+
[@@unboxed]
6164
(** nullable, value of this type can be either [null] or ['a]
6265
this type is the same as type [t] in {!Null}
6366
*)
@@ -66,7 +69,11 @@ type + 'a undefined
6669
(** value of this type can be either [undefined] or ['a]
6770
this type is the same as type [t] in {!Undefined} *)
6871

69-
type + 'a nullable
72+
type + 'a nullable =
73+
| Value of 'a
74+
| Null [@as null]
75+
| Undefined [@as undefined]
76+
[@@unboxed]
7077
(** value of this type can be [undefined], [null] or ['a]
7178
this type is the same as type [t] n {!Null_undefined} *)
7279

jscomp/test/custom_error_test.js

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/test/js_json_test.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)