Skip to content

Commit cc5c2c6

Browse files
committed
Expose Id.MakeComparable/ComparableU/Hashable/HashableU
This allows the creation of custom Set/Map through these functor directly rather than passing by a first-class module. Slightly more verbose, but much easier for newcomers.
1 parent 874539e commit cc5c2c6

File tree

2 files changed

+45
-14
lines changed

2 files changed

+45
-14
lines changed

jscomp/others/belt_Id.ml

Lines changed: 12 additions & 2 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,11 +53,21 @@ 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+
let cmp = (fun[@bs] a b -> M.cmp a b)
64+
end
65+
5666
let comparableU
5767
(type key)
5868
cmp
5969
=
60-
let module N = MakeComparable(struct
70+
let module N = MakeComparableU(struct
6171
type t = key
6272
let cmp = cmp
6373
end) in

jscomp/others/belt_Id.mli

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,26 @@
3535

3636

3737

38-
type ('a, 'id) hash
38+
type ('a, 'id) hash = 'a -> int [@bs]
3939
(** [('a, 'id) hash]
4040
4141
Its runtime represenation is a [hash] function, but signed with a
4242
type parameter, so that different hash functions type mismatch
4343
*)
4444

45-
type ('a, 'id) eq
45+
type ('a, 'id) eq = 'a -> 'a -> bool [@bs]
4646
(** [('a, 'id) eq]
4747
4848
Its runtime represenation is an [eq] function, but signed with a
4949
type parameter, so that different hash functions type mismatch
5050
*)
5151

52-
type ('a, 'id) cmp
52+
type ('a, 'id) cmp = 'a -> 'a -> int [@bs]
5353
(** [('a,'id) cmp]
5454
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,6 +74,28 @@ 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+
sig
83+
type identity
84+
type t = M.t
85+
val cmp : M.t -> M.t -> int [@bs]
86+
end
87+
88+
module MakeComparable :
89+
functor (M : sig
90+
type t
91+
val cmp : t -> t -> int
92+
end) ->
93+
sig
94+
type identity
95+
type t = M.t
96+
val cmp : M.t -> M.t -> int [@bs]
97+
end
98+
7899
val comparableU:
79100
('a -> 'a -> int [@bs]) ->
80101
(module Comparable with type t = 'a)
@@ -84,11 +105,11 @@ val comparable:
84105
(module Comparable with type t = 'a)
85106

86107
module type Hashable = sig
87-
type identity
88-
type t
89-
val hash: (t,identity) hash
90-
val eq: (t,identity) eq
91-
end
108+
type identity
109+
type t
110+
val hash : (t, identity) hash
111+
val eq : (t, identity) eq
112+
end
92113

93114
type ('key, 'id) hashable =
94115
(module Hashable with type t = 'key and type identity = 'id)
@@ -105,13 +126,13 @@ type ('key, 'id) hashable =
105126

106127

107128

108-
val hashableU:
129+
val hashableU :
109130
hash:('a -> int [@bs]) ->
110131
eq:('a -> 'a -> bool [@bs]) ->
111132
(module Hashable with type t = 'a)
112133

113-
val hashable:
114-
hash:('a -> int ) ->
134+
val hashable :
135+
hash:('a -> int) ->
115136
eq:('a -> 'a -> bool ) ->
116137
(module Hashable with type t = 'a)
117138

0 commit comments

Comments
 (0)