From: Michal M. <mal...@pl...> - 2003-06-17 09:56:05
|
On Tue, Jun 17, 2003 at 04:41:58PM +0900, Nicolas Cannasse wrote: > > What about: > > > > let s1 = ExtSet.make cmp1 in > > let s2 = ExtSet.make cmp2 in > > ... > > let s = ExtSet.union s1 s2 > > Should raise something like "exception Comparator_differs" if not physical > equality between s1.cmp and s2.cmp. Physical equality of functions can be tricky. Example: # let g () = fun x -> x;; val g : unit -> 'a -> 'a = <fun> # g () == g ();; - : bool = false It is possible that you define comparator function to be local (but not to depend on environment), and your runtime check will fail. let f () = let cmp a b = a.x - b.x in let s = ExtSet.make cmp in ... s > in functor approach, you cannot actually do it since the comparator is bound > to a type/module so errors are detected at compilation time and not at > runtime. Well, I have to admit that I like the compile-time check a lot more. > But that's a small tradeoff compared to the ability to define sets > without having to define a module for every type you need. Defining module isn't as painful :-) It can be in fact done in single line: module Iset = Set.Make(struct type t = int let compare = compare end) -- : Michal Moskal :: http://www.kernel.pl/~malekith : GCS {C,UL}++++$ a? !tv : When in doubt, use brute force. -- Ken Thompson : {E-,w}-- {b++,e}>+++ h |