File: examples.mlt

package info (click to toggle)
ppx-hash 0.17.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 316 kB
  • sloc: ml: 1,961; ansic: 180; makefile: 14; sh: 11
file content (229 lines) | stat: -rw-r--r-- 4,776 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
open Ppx_hash_lib.Std
open Hash.Builtin;;

#verbose true

module type Interface_for_types_named_t = sig
  type t [@@deriving hash]
end

[%%expect
  {|
module type Interface_for_types_named_t =
  sig
    type t
    val hash_fold_t :
      t Base.Exported_for_specific_uses.Ppx_hash_lib.hash_fold
    val hash : t -> int
  end
|}]

module type Interface_for_types_named_other_than_t = sig
  type my_type [@@deriving hash]
end

[%%expect
  {|
module type Interface_for_types_named_other_than_t =
  sig
    type my_type
    val hash_fold_my_type :
      Base_internalhash_types.state ->
      my_type -> Base_internalhash_types.state
    val hash_my_type : my_type -> int
  end
|}]

module type Interface_for_poly_types = sig
  type 'a my_container [@@deriving hash]
end

[%%expect
  {|
module type Interface_for_poly_types =
  sig
    type 'a my_container
    val hash_fold_my_container :
      (Base_internalhash_types.state -> 'a -> Base_internalhash_types.state) ->
      Base_internalhash_types.state ->
      'a my_container -> Base_internalhash_types.state
  end
|}]

module type Interface_for_poly_types_with_unnamed_argument = sig
  type _ my_container [@@deriving hash]
end

[%%expect
  {|
module type Interface_for_poly_types_with_unnamed_argument =
  sig
    type _ my_container
    val hash_fold_my_container :
      (Base_internalhash_types.state ->
       'a__001_ -> Base_internalhash_types.state) ->
      Base_internalhash_types.state ->
      'a__001_ my_container -> Base_internalhash_types.state
  end
|}]

module Use_of_hash_fold_syntax_extension = struct
  let f = [%hash_fold: (int * string) list]
end

[%%expect
  {|
module Use_of_hash_fold_syntax_extension :
  sig
    val f :
      Base_internalhash_types.state ->
      (int * string) list -> Base_internalhash_types.state
  end
|}]

module Support_for_builtins = struct
  let f =
    [%hash_fold:
      (nativeint * int64 * int32 * char * int * bool * string * float * unit) option list
        lazy_t]
  ;;
end

[%%expect
  {|
module Support_for_builtins :
  sig
    val f :
      Base_internalhash_types.state ->
      (nativeint * int64 * int32 * char * int * bool * string * float * unit)
      option list lazy_t -> Base_internalhash_types.state
  end
|}]

(* negative tests... *)

module No_builtin_support_for_array = struct
  type fail = int array [@@deriving hash]
end

[%%expect
  {|
Line _, characters _-_:
Error: Unbound value hash_fold_array
|}]

module No_builtin_support_for_ref = struct
  type fail = int ref [@@deriving hash]
end

[%%expect
  {|
Line _, characters _-_:
Error: Unbound value hash_fold_ref
Hint: Did you mean hash_fold_int?
|}]

type fail = int -> int [@@deriving hash]

[%%expect
  {|
Line _, characters _-_:
Error: ppx_hash: functions can not be hashed.
|}]

type fail = < f : int > [@@deriving hash]

[%%expect
  {|
Line _, characters _-_:
Error: ppx_hash: unsupported type: < f: int   >
|}]

type fail = .. [@@deriving hash]

[%%expect
  {|
Line _, characters _-_:
Error: ppx_hash: open types are not supported
|}]

type fail = private [> `Foo ] [@@deriving hash]

[%%expect
  {|
Line _, characters _-_:
Error: ppx_hash: cannot hash open polymorphic variant types
|}]

type fail =
  { mutable u : int
  ; s : string
  }
[@@deriving hash]

[%%expect
  {|
Line _, characters _-_:
Error: ppx_hash: require [@hash.ignore] or [@compare.ignore] on mutable
       record field
|}]

type unhashable

type ok =
  { mutable u : unhashable [@compare.ignore]
  ; s : string
  }
[@@deriving hash]

[%%expect
  {|
type unhashable
type ok = { mutable u : unhashable; s : string; }
val hash_fold_ok :
  Base_internalhash_types.state -> ok -> Base_internalhash_types.state =
  <fun>
val hash_ok : ok -> int = <fun>
|}]

type ok_nested =
  { u : (unhashable[@ignore]) * int
  ; s : string
  }
[@@deriving hash]

[%%expect
  {|
type ok_nested = { u : unhashable * int; s : string; }
val hash_fold_ok_nested :
  Base_internalhash_types.state -> ok_nested -> Base_internalhash_types.state =
  <fun>
val hash_ok_nested : ok_nested -> int = <fun>
|}]

module type Type_extension = sig
  val f : [%hash_fold: 'a] -> [%hash_fold: 'a * 'a]
  val g : [%hash_fold: _] -> [%hash_fold: _]
end

[%%expect
  {|
module type Type_extension =
  sig
    val f :
      (Base_internalhash_types.state -> 'a -> Base_internalhash_types.state) ->
      Base_internalhash_types.state ->
      'a * 'a -> Base_internalhash_types.state
    val g :
      (Base_internalhash_types.state -> 'a -> Base_internalhash_types.state) ->
      Base_internalhash_types.state -> 'b -> Base_internalhash_types.state
  end
|}]

type warn = { foo : string [@no_hashing] } [@@deriving hash]

[%%expect
  {|
Line _, characters _-_:
Error (warning 22 [preprocessor]): [@hash.no_hashing] is deprecated.  Use [@hash.ignore].
|}]