Permalink
Browse files

an example of functor application for Hashtbl

(Patch by Simon Cruanes)

git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@14644 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
  • Loading branch information...
gasche committed Apr 19, 2014
1 parent d3ec5fe commit 5ef784b7b23561d6732a7cd52441c4dae5210640
Showing with 21 additions and 0 deletions.
  1. +21 −0 stdlib/hashtbl.mli
View
@@ -187,6 +187,27 @@ val stats : ('a, 'b) t -> statistics
(** {6 Functorial interface} *)
+(** The functorial interface allows to use specific comparison
+ and hash functions, either for performance/security concerns,
+ or because keys are not hashable/comparable with the polymorphic builtins.
+
+ For instance, one might want to specialize a table for integer keys:
+ {[
+ module IntHash =
+ struct
+ type t = int
+ let equal i j = i=j
+ let hash i = i land max_int
+ end
+
+ module IntHashtbl = Hashtbl.Make(IntHash)
+
+ let h = IntHashtbl.create 17 in
+ IntHashtbl.add h 12 "hello";; (* works *)
+
+ let h' = IntHashtbl.create 17 in
+ IntHashtbl.add h false "world";; (* won't typecheck *)
+ ]} *)
module type HashedType =
sig

0 comments on commit 5ef784b

Please sign in to comment.