Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] polymorphic modules in classes
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: [Caml-list] polymorphic modules in classes
From: Axel Simon <A.Simon@ukc.ac.uk>

> When I compile, I get "Unbound type constructor a'" for the line which 
> introduces stored, although it works with int.

This just looks like a typo: this should be 'a (a type variable), not
a' (an identifier). When fixed, your program is accepted.

> I guess there are many 
> other problems with my solution: PhysEq.t is abstract and PhysHashtbl.key 
> is not bound to any type. How do I solve all this?

This one is a much more serious problem, and appears frequently on
this list.
The only solution currently is to copy the Hashtbl module, replacing
key by 'a key, and 'a t by ('b,'a) t.

A simpler workaround is to define a functor producing your class.

module Make(T : sig type t end) = struct
  module PhysEq = struct
    type t = T.t
    let equal = (==)
    let hash  = H.hash
  end

  module PhysHashtbl : H.S with type key = T.t = H.Make(PhysEq)

  class ['a] bulkWriter ((fname, wr) : string * (out_channel -> 'a -> unit))  =
    let outCh = open_out_bin fname
    and stored   : ('a PhysHashtbl.t) = (PhysHashtbl.create 100)
    in object
     (* something here *)   
    end
end

Then you can create a class for every type you need.

But I'm not sure exactly of what you intend to use your hash table
for. Does it really need to handle many types?

Jacques Garrigue

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners