caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Hakan Suka <hakan40us@yahoo.com>
To: caml-list@inria.fr
Subject: [Caml-list] functor inside a class
Date: Mon, 23 May 2011 04:17:00 -0700 (PDT)	[thread overview]
Message-ID: <172756.45853.qm@web120203.mail.ne1.yahoo.com> (raw)

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

             reply	other threads:[~2011-05-23 11:17 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-23 11:17 Hakan Suka [this message]
2011-05-23 19:34 ` Guillaume Yziquel
2011-05-23 19:44   ` Ashish Agarwal
2011-05-23 21:58 ` Gabriel Scherer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=172756.45853.qm@web120203.mail.ne1.yahoo.com \
    --to=hakan40us@yahoo.com \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).