* class type inside a sig
@ 2005-07-06 9:20 Pietro Abate
2005-07-06 10:13 ` [Caml-list] " Jacques Garrigue
0 siblings, 1 reply; 2+ messages in thread
From: Pietro Abate @ 2005-07-06 9:20 UTC (permalink / raw)
To: ocaml ml
Hi all,
how can I pass a generic class type inside a signature without
actually specifying the class itself ?
this is a broken example :
module type S = sig type t end
module Make ( T:S ) = struct
let init = new T.t
let add s e = s#add e
end
this is the best I figured out :
module type S =
sig
type c = < add : int -> 'c > as 'c
val make : unit -> c
end
module Make ( T:S ) = struct
(* let init = new T.c *)
let init = T.make ()
let add e = init#add e
end
but it isn't satisfactory as I've to pass a 'make' function and
if I try to instantiate the module with a super class of c, it gives
me a signature error. For example:
class test : c =
object
val data = []
method add (e :int) = {< data = e::data >}
method other = 1
end
module A = Make (struct class c = test let make () = new test end)
doesn't work because
Signature mismatch:
Modules do not match:
sig class c : test val make : unit -> test end
is not included in
S
Type declarations do not match:
type c = < add : int -> c; other : int >
is not included in
type c = < add : int -> 'a > as 'a
the class c in the signature should be a class that has 'at least' a
method add with respect to the signature, but it can actually be any
arbitrary class...
how can I achieve this ?
Basically I'd like to say:
type c = < add : int -> 'a; .. > as 'a
but this doesn't work...
:)
p
--
++ Blog: http://blog.rsise.anu.edu.au/?q=pietro
++
++ "All great truths begin as blasphemies." -George Bernard Shaw
++ Please avoid sending me Word or PowerPoint attachments.
See http://www.fsf.org/philosophy/no-word-attachments.html
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [Caml-list] class type inside a sig
2005-07-06 9:20 class type inside a sig Pietro Abate
@ 2005-07-06 10:13 ` Jacques Garrigue
0 siblings, 0 replies; 2+ messages in thread
From: Jacques Garrigue @ 2005-07-06 10:13 UTC (permalink / raw)
To: Pietro.Abate; +Cc: caml-list
From: Pietro Abate <Pietro.Abate@anu.edu.au>
> how can I pass a generic class type inside a signature without
> actually specifying the class itself ?
>
> this is the best I figured out :
>
> module type S =
> sig
> type c = < add : int -> 'c > as 'c
> val make : unit -> c
> end
>
> module Make ( T:S ) = struct
>
> (* let init = new T.c *)
> let init = T.make ()
> let add e = init#add e
>
> end
>
> but it isn't satisfactory as I've to pass a 'make' function and
This is somehow unavoidable: if you want your class type to be
generic, then you cannot be sure that its constructor will have the
right type. So it is more natural to pass it independently.
> if I try to instantiate the module with a super class of c, it gives
> me a signature error.
[...]
> Signature mismatch:
> Modules do not match:
> sig class c : test val make : unit -> test end
> is not included in
> S
> Type declarations do not match:
> type c = < add : int -> c; other : int >
> is not included in
> type c = < add : int -> 'a > as 'a
>
> how can I achieve this ?
>
> Basically I'd like to say:
> type c = < add : int -> 'a; .. > as 'a
Impossible in ocaml 3.08.3.
With ocaml 3.09 (CVS version)
type c = private < add : int -> 'a; .. > as 'a
is exactly what you are asking for.
And before someone asks, you of course cannot inherit from such a
generic class (even if it were declared as a class rather than an
object type), as there is no way to know what other public methods it
could (i.e. they might be incompatible to the ones you add inside your
functor.)
Jacques
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2005-07-06 10:13 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-07-06 9:20 class type inside a sig Pietro Abate
2005-07-06 10:13 ` [Caml-list] " Jacques Garrigue
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).