From: Guillaume Yziquel <guillaume.yziquel@citycable.ch>
To: Hakan Suka <hakan40us@yahoo.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] functor inside a class
Date: Mon, 23 May 2011 21:34:13 +0200 [thread overview]
Message-ID: <20110523193413.GA22132@localhost> (raw)
In-Reply-To: <172756.45853.qm@web120203.mail.ne1.yahoo.com>
Le Monday 23 May 2011 à 04:17:00 (-0700), Hakan Suka a écrit :
> Hi everyone,
>
> 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
First of all, the scope in which you can refer to LabelSet is local to
the class declaration. However, the class method get has a type which
refers to LabelSet. That's out of scope.
More importantly, it seems that
let f x = let module M = struct end in x
is syntactically valid, while it doesn't seem to work out the same way for
class declarations:
class ['a] tracker = fun size -> let module M = struct end in object end
gives a syntax error.
You should create your local module somewhere else than before 'object'
'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?
The big point is that you need the 'get' method to return something which
is a 't' element of a module satisfying the 'Set.S with type t = 'a'
signature.
You need a type constructor for that. Set.Make(String).t is a valid
type, but I do not see how parametrise String for some other 'a type.
The following works:
type 'a set_module = (module Set.S with type elt = 'a)
let make_set (type s) ?(cmp = compare) () =
let module S = Set.Make (struct type t = s let compare = cmp end) in
(module S : Set.S with type elt = s)
class ['a] tracker size = object
val set = make_set ()
val nums_to_track_tbl : (int32, 'a set_module) Hashtbl.t = Hashtbl.create size
method get pos = Hashtbl.find_all nums_to_track_tbl pos
end
But it's a 'a set_module, not a 'a set, so first-class modules do not
seem so useful at first glance...
What you'd need is not a
type 'a set_module = (module Set.S with type elt = 'a)
but something like
type 'a set = (module Set.S with type elt = 'a).t
(which is not valid), and to my knowledge, this cannot be done.
However, I think there is a polymorphic-not-functorised code for maps
and sets lying out somewhere on the net. This is likely your best bet.
> Thanks,
> Hakan
--
Guillaume Yziquel
next prev parent reply other threads:[~2011-05-23 19:36 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-05-23 11:17 Hakan Suka
2011-05-23 19:34 ` Guillaume Yziquel [this message]
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=20110523193413.GA22132@localhost \
--to=guillaume.yziquel@citycable.ch \
--cc=caml-list@inria.fr \
--cc=hakan40us@yahoo.com \
/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