caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] functor inside a class
@ 2011-05-23 11:17 Hakan Suka
  2011-05-23 19:34 ` Guillaume Yziquel
  2011-05-23 21:58 ` Gabriel Scherer
  0 siblings, 2 replies; 4+ messages in thread
From: Hakan Suka @ 2011-05-23 11:17 UTC (permalink / raw)
  To: caml-list

Hi everyone,

I tried the ocaml-beginners list with this question but did not get a good 
solution, so I am retrying here.

I want to create a parameterised class that takes as input a polymorphic "label" 

type and internally stores information using a Set of that type. However, I 
cannot get the functor to work inside a class, so I wonder whether this is 
possible.

For example, the following works:

module LabelSet =
Set.Make (struct type t = int let compare = compare end)

class ['a] tracker size =
object

val nums_to_track_tbl : (int32, LabelSet.t) Hashtbl.t =
Hashtbl.create size

method get pos = Hashtbl.find_all nums_to_track_tbl pos
end

But can I have a local module definition so that the I can have a tracker not 
only for integers, but for any type being passed as input? The following does 
not work:

class ['a] tracker size =
let module LabelSet =
Set.Make (struct type t = 'a let compare = compare end)
in
object
val nums_to_track_tbl : (int32, LabelSet.t) Hashtbl.t =
Hashtbl.create size

method get pos = Hashtbl.find_all nums_to_track_tbl pos

end

If this is not possible, would it work in Ocaml 3.12 with first-class modules?

I have seen the following code in the manual which seems fairly close of what I 
would need inside the parametrised class, but not sure if I can use the output 
of make_set inside the module. I haven't tested this because I have Ocaml 3.11.2

let make_set (type s) cmp =
let module S = Set.Make(struct
type t = s
let compare = cmp
end) in
(module S : Set.S with type elt = s) 

If that does not work, any suggestions on how to implement this?

Thanks,
Hakan

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2011-05-23 21:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-23 11:17 [Caml-list] functor inside a class Hakan Suka
2011-05-23 19:34 ` Guillaume Yziquel
2011-05-23 19:44   ` Ashish Agarwal
2011-05-23 21:58 ` Gabriel Scherer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).