Skip to content

Commit 289cf1d

Browse files
authored
Merge pull request #2589 from chenglou/functor
Expose Id.MakeComparable/ComparableU/Hashable/HashableU
2 parents dbd0ed8 + a9f650a commit 289cf1d

File tree

3 files changed

+135
-28
lines changed

3 files changed

+135
-28
lines changed

jscomp/others/belt_Id.ml

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type ('key, 'id) comparable =
4343
(module Comparable with type t = 'key and type identity = 'id)
4444

4545

46-
module MakeComparable (M : sig
46+
module MakeComparableU (M : sig
4747
type t
4848
val cmp: t -> t -> int [@bs]
4949
end) =
@@ -53,20 +53,38 @@ struct
5353
let cmp = M.cmp
5454
end
5555

56+
module MakeComparable (M : sig
57+
type t
58+
val cmp: t -> t -> int
59+
end) =
60+
struct
61+
type identity
62+
type t = M.t
63+
(* see https://github.com/BuckleScript/bucklescript/pull/2589/files/5ef875b7665ee08cfdc59af368fc52bac1fe9130#r173330825 *)
64+
let cmp =
65+
let cmp = M.cmp in fun[@bs] a b -> cmp a b
66+
end
67+
5668
let comparableU
5769
(type key)
5870
cmp
5971
=
60-
let module N = MakeComparable(struct
72+
let module N = MakeComparableU(struct
6173
type t = key
6274
let cmp = cmp
6375
end) in
6476
(module N : Comparable with type t = key)
6577

78+
let comparable
79+
(type key)
80+
cmp
81+
=
82+
let module N = MakeComparable(struct
83+
type t = key
84+
let cmp = cmp
85+
end) in
86+
(module N : Comparable with type t = key)
6687

67-
let comparable cmp =
68-
comparableU (fun[@bs] a b -> cmp a b)
69-
7088
module type Hashable = sig
7189
type identity
7290
type t
@@ -76,9 +94,9 @@ end
7694

7795
type ('key, 'id) hashable = (module Hashable with type t = 'key and type identity = 'id)
7896

79-
module MakeHashable (M : sig
97+
module MakeHashableU (M : sig
8098
type t
81-
val hash : t -> int [@bs]
99+
val hash : t -> int [@bs]
82100
val eq : t -> t -> bool [@bs]
83101
end) =
84102
struct
@@ -87,16 +105,33 @@ struct
87105
let hash = M.hash
88106
let eq = M.eq
89107
end
90-
108+
109+
module MakeHashable (M : sig
110+
type t
111+
val hash : t -> int
112+
val eq : t -> t -> bool
113+
end) =
114+
struct
115+
type identity
116+
type t = M.t
117+
let hash =
118+
let hash = M.hash in fun[@bs] a -> hash a
119+
let eq =
120+
let eq = M.eq in fun[@bs] a b -> eq a b
121+
end
122+
91123
let hashableU (type key) ~hash ~eq =
92-
let module N = MakeHashable(struct
124+
let module N = MakeHashableU(struct
93125
type t = key
94126
let hash = hash
95127
let eq = eq
96128
end) in
97129
(module N : Hashable with type t = key)
98130

99-
let hashable ~hash ~eq =
100-
hashableU
101-
~hash:(fun [@bs] a -> hash a)
102-
~eq:(fun [@bs] a b -> eq a b)
131+
let hashable (type key) ~hash ~eq =
132+
let module N = MakeHashable(struct
133+
type t = key
134+
let hash = hash
135+
let eq = eq
136+
end) in
137+
(module N : Hashable with type t = key)

jscomp/others/belt_Id.mli

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type ('a, 'id) hash
4242
type parameter, so that different hash functions type mismatch
4343
*)
4444

45-
type ('a, 'id) eq
45+
type ('a, 'id) eq
4646
(** [('a, 'id) eq]
4747
4848
Its runtime represenation is an [eq] function, but signed with a
@@ -55,7 +55,6 @@ type ('a, 'id) cmp
5555
Its runtime representation is a [cmp] function, but signed with a
5656
type parameter, so that different hash functions type mismatch
5757
*)
58-
5958
module type Comparable = sig
6059
type identity
6160
type t
@@ -75,20 +74,36 @@ type ('key, 'id) comparable =
7574
mismatch if they use different comparison function
7675
*)
7776

77+
module MakeComparableU :
78+
functor (M : sig
79+
type t
80+
val cmp : t -> t -> int [@bs]
81+
end) ->
82+
Comparable with type t = M.t
83+
84+
module MakeComparable :
85+
functor (M : sig
86+
type t
87+
val cmp : t -> t -> int
88+
end) ->
89+
Comparable with type t = M.t
90+
7891
val comparableU:
7992
('a -> 'a -> int [@bs]) ->
8093
(module Comparable with type t = 'a)
94+
[@@ocaml.deprecated "Use the MakeComparableU functor API instead"]
8195

8296
val comparable:
8397
('a -> 'a -> int) ->
8498
(module Comparable with type t = 'a)
99+
[@@ocaml.deprecated "Use the MakeComparable functor API instead"]
85100

86101
module type Hashable = sig
87-
type identity
88-
type t
89-
val hash: (t,identity) hash
90-
val eq: (t,identity) eq
91-
end
102+
type identity
103+
type t
104+
val hash : (t, identity) hash
105+
val eq : (t, identity) eq
106+
end
92107

93108
type ('key, 'id) hashable =
94109
(module Hashable with type t = 'key and type identity = 'id)
@@ -103,17 +118,33 @@ type ('key, 'id) hashable =
103118
mismatch if they use different comparison function
104119
*)
105120

106-
107-
108-
val hashableU:
121+
module MakeHashableU :
122+
functor (M : sig
123+
type t
124+
val hash : t -> int [@bs]
125+
val eq : t -> t -> bool [@bs]
126+
end) ->
127+
Hashable with type t = M.t
128+
129+
module MakeHashable :
130+
functor (M : sig
131+
type t
132+
val hash : t -> int
133+
val eq : t -> t -> bool
134+
end) ->
135+
Hashable with type t = M.t
136+
137+
val hashableU :
109138
hash:('a -> int [@bs]) ->
110139
eq:('a -> 'a -> bool [@bs]) ->
111140
(module Hashable with type t = 'a)
112-
113-
val hashable:
114-
hash:('a -> int ) ->
141+
[@@ocaml.deprecated "Use the MakeHashableU functor API instead"]
142+
143+
val hashable :
144+
hash:('a -> int) ->
115145
eq:('a -> 'a -> bool ) ->
116146
(module Hashable with type t = 'a)
147+
[@@ocaml.deprecated "Use the MakeHashable functor API instead"]
117148

118149

119150

lib/js/belt_Id.js

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,44 @@
22

33
var Curry = require("./curry.js");
44

5+
function MakeComparableU(M) {
6+
var cmp = M[/* cmp */0];
7+
return /* module */[/* cmp */cmp];
8+
}
9+
10+
function MakeComparable(M) {
11+
var cmp = M[/* cmp */0];
12+
var cmp$1 = Curry.__2(cmp);
13+
return /* module */[/* cmp */cmp$1];
14+
}
15+
516
function comparableU(cmp) {
617
return /* module */[/* cmp */cmp];
718
}
819

920
function comparable(cmp) {
10-
return comparableU(Curry.__2(cmp));
21+
var cmp$1 = Curry.__2(cmp);
22+
return /* module */[/* cmp */cmp$1];
23+
}
24+
25+
function MakeHashableU(M) {
26+
var hash = M[/* hash */0];
27+
var eq = M[/* eq */1];
28+
return /* module */[
29+
/* hash */hash,
30+
/* eq */eq
31+
];
32+
}
33+
34+
function MakeHashable(M) {
35+
var hash = M[/* hash */0];
36+
var hash$1 = Curry.__1(hash);
37+
var eq = M[/* eq */1];
38+
var eq$1 = Curry.__2(eq);
39+
return /* module */[
40+
/* hash */hash$1,
41+
/* eq */eq$1
42+
];
1143
}
1244

1345
function hashableU(hash, eq) {
@@ -18,11 +50,20 @@ function hashableU(hash, eq) {
1850
}
1951

2052
function hashable(hash, eq) {
21-
return hashableU(Curry.__1(hash), Curry.__2(eq));
53+
var hash$1 = Curry.__1(hash);
54+
var eq$1 = Curry.__2(eq);
55+
return /* module */[
56+
/* hash */hash$1,
57+
/* eq */eq$1
58+
];
2259
}
2360

61+
exports.MakeComparableU = MakeComparableU;
62+
exports.MakeComparable = MakeComparable;
2463
exports.comparableU = comparableU;
2564
exports.comparable = comparable;
65+
exports.MakeHashableU = MakeHashableU;
66+
exports.MakeHashable = MakeHashable;
2667
exports.hashableU = hashableU;
2768
exports.hashable = hashable;
2869
/* No side effect */

0 commit comments

Comments
 (0)