Skip to content

Commit d206b2d

Browse files
authored
Dict as a builtin (#6590)
* dict as a builtin type * fix * revert unintended changes * adapt gentype to dict as built in * changelog
1 parent b88e74f commit d206b2d

File tree

9 files changed

+20
-8
lines changed

9 files changed

+20
-8
lines changed

CHANGELOG.md

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

1717
- Experimental support of tagged template literals, eg ```sql`select * from ${table}```. https://github.com/rescript-lang/rescript-compiler/pull/6250
1818
- Experimental support for generic/custom JSX transforms. https://github.com/rescript-lang/rescript-compiler/pull/6565
19+
- `dict` is now a builtin type. https://github.com/rescript-lang/rescript-compiler/pull/6590
1920

2021
#### :bug: Bug Fix
2122

jscomp/gentype/TranslateTypeExprFromTypes.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ let translateConstr ~config ~paramsTranslation ~(path : Path.t) ~typeEnv =
236236
| ( (["Js"; "Promise"; "t"] | ["Promise"; "t"] | ["promise"]),
237237
[paramTranslation] ) ->
238238
{paramTranslation with type_ = Promise paramTranslation.type_}
239-
| (["Js"; "Dict"; "t"] | ["Dict"; "t"]), [paramTranslation] ->
239+
| (["Js"; "Dict"; "t"] | ["Dict"; "t"] | ["dict"]), [paramTranslation] ->
240240
{paramTranslation with type_ = Dict paramTranslation.type_}
241241
| ["function$"], [arg; _arity] ->
242242
{dependencies = arg.dependencies; type_ = arg.type_}

jscomp/ml/ast_untagged_variants.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ let reportConstructorMoreThanOneArg ~loc ~name =
142142

143143
let type_is_builtin_object (t : Types.type_expr) =
144144
match t.desc with
145+
| Tconstr (Path.Pident ident, [_], _) when Ident.name ident = "dict" -> true
145146
| Tconstr (path, _, _) ->
146147
let name = Path.name path in
147148
name = "Js.Dict.t" || name = "Js_dict.t"

jscomp/ml/predef.ml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ and ident_array = ident_create "array"
4040
and ident_list = ident_create "list"
4141
and ident_option = ident_create "option"
4242
and ident_result = ident_create "result"
43+
and ident_dict = ident_create "dict"
4344

4445
and ident_int64 = ident_create "int64"
4546
and ident_lazy_t = ident_create "lazy_t"
@@ -82,6 +83,7 @@ and path_array = Pident ident_array
8283
and path_list = Pident ident_list
8384
and path_option = Pident ident_option
8485
and path_result = Pident ident_result
86+
and path_dict = Pident ident_dict
8587

8688

8789
and path_int64 = Pident ident_int64
@@ -105,6 +107,7 @@ and type_array t = newgenty (Tconstr(path_array, [t], ref Mnil))
105107
and type_list t = newgenty (Tconstr(path_list, [t], ref Mnil))
106108
and type_option t = newgenty (Tconstr(path_option, [t], ref Mnil))
107109
and type_result t1 t2 = newgenty (Tconstr(path_result, [t1; t2], ref Mnil))
110+
and type_dict t = newgenty (Tconstr(path_dict, [t], ref Mnil))
108111

109112
and type_int64 = newgenty (Tconstr(path_int64, [], ref Mnil))
110113
and type_lazy_t t = newgenty (Tconstr(path_lazy_t, [t], ref Mnil))
@@ -226,6 +229,12 @@ let common_initial_env add_type add_extension empty_env =
226229
Type_variant([cstr ident_ok [tvar1];
227230
cstr ident_error [tvar2]]);
228231
type_variance = [Variance.covariant; Variance.covariant]}
232+
and decl_dict =
233+
let tvar = newgenvar() in
234+
{decl_abstr with
235+
type_params = [tvar];
236+
type_arity = 1;
237+
type_variance = [Variance.covariant]}
229238
and decl_uncurried =
230239
let tvar1, tvar2 = newgenvar(), newgenvar() in
231240
{decl_abstr with
@@ -292,6 +301,7 @@ let common_initial_env add_type add_extension empty_env =
292301
add_type ident_lazy_t decl_lazy_t (
293302
add_type ident_option decl_option (
294303
add_type ident_result decl_result (
304+
add_type ident_dict decl_dict (
295305
add_type ident_list decl_list (
296306
add_type ident_array decl_array (
297307
add_type ident_exn decl_exn (
@@ -305,7 +315,7 @@ let common_initial_env add_type add_extension empty_env =
305315
add_type ident_extension_constructor decl_abstr (
306316
add_type ident_floatarray decl_abstr (
307317
add_type ident_promise decl_promise (
308-
empty_env))))))))))))))))))))))))))
318+
empty_env)))))))))))))))))))))))))))
309319

310320
let build_initial_env add_type add_exception empty_env =
311321
let common = common_initial_env add_type add_exception empty_env in

jscomp/ml/predef.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ val type_array: type_expr -> type_expr
2929
val type_list: type_expr -> type_expr
3030
val type_option: type_expr -> type_expr
3131
val type_result: type_expr -> type_expr -> type_expr
32+
val type_dict: type_expr -> type_expr
3233

3334
val type_int64: type_expr
3435
val type_lazy_t: type_expr -> type_expr
@@ -47,6 +48,7 @@ val path_array: Path.t
4748
val path_list: Path.t
4849
val path_option: Path.t
4950
val path_result: Path.t
51+
val path_dict: Path.t
5052

5153
val path_int64: Path.t
5254
val path_lazy_t: Path.t

jscomp/others/js_dict.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
/*** Provides a simple key-value dictionary abstraction over native JavaScript objects */
2626

2727
/** The dict type */
28-
type t<'a>
28+
type t<'a> = dict<'a>
2929

3030
/** The key type, an alias of string */
3131
type key = string

jscomp/others/js_dict.resi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Dictionary type (ie an '{ }' JS object). However it is restricted to hold a
3939
single type; therefore values must have the same type. This Dictionary type is
4040
mostly used with the Js_json.t type.
4141
*/
42-
type t<'a>
42+
type t<'a> = dict<'a>
4343

4444
/**
4545
The type for dictionary keys. This means that dictionaries *must* use `string`s as their keys.

lib/es6/js_json.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11

22

3-
import * as Caml_option from "./caml_option.js";
43

54
var Kind = {};
65

@@ -75,7 +74,7 @@ function decodeNumber(json) {
7574

7675
function decodeObject(json) {
7776
if (typeof json === "object" && !Array.isArray(json) && json !== null) {
78-
return Caml_option.some(json);
77+
return json;
7978
}
8079

8180
}

lib/js/js_json.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
var Caml_option = require("./caml_option.js");
43

54
var Kind = {};
65

@@ -75,7 +74,7 @@ function decodeNumber(json) {
7574

7675
function decodeObject(json) {
7776
if (typeof json === "object" && !Array.isArray(json) && json !== null) {
78-
return Caml_option.some(json);
77+
return json;
7978
}
8079

8180
}

0 commit comments

Comments
 (0)