From: Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>
To: Damien.Pous@ens-lyon.fr
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] functors and objects
Date: Wed, 04 Feb 2004 11:57:45 +0900 [thread overview]
Message-ID: <20040204115745J.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: <20040203190635.6cc61ff0.Damien.Pous@ens-lyon.fr>
From: Damien <Damien.Pous@ens-lyon.fr>
> I would like to write something like
>
> <<
> class type o =
> object
> method react: unit
> end
>
> module type O =
> sig
> type t :> o (* sigh... *)
> end
>
> module R(M: O) =
> struct
> let l: M.t list ref = []
> let register (o: M.t) = l := o :: !l
> let react() = List.iter (fun o -> o#react) !l
> end
> >>
The closest I can see to what you ask for is
module type O = sig
type t
val as_o : t -> o
end
module R(M: O) = struct
let l: M.t list ref = ref []
let register (o: M.t) = l := o :: !l
let react() = List.iter (fun o -> (M.as_o o)#react) !l
end
module RO = R(struct type t = o let as_o x = x end)
Then your second layer would be
class type o' = object
inherit o
method render: unit
end
module type O' = sig
include O
val as_o' : t -> o'
end
module R(M: O') = struct
include R(M)
let render() = List.iter (fun o -> (M.as_o' o)#render) !l
end
module O' = struct
type t = o'
let as_o x = (x : t :> o)
let as_o' x = x
end
module RO' = R(O')
> Is it unsound to let a functor use an object type ?
> (not to inherit from the class,
> just to use the methods of objects belonging to this type)
This isn't a problem of soundness.
There is just no such thing as a "partially abstract" object type.
But as shown above, you can easily simulate it by coupling an abstract
type with a coercion to an object type.
Note however that it would be probably simpler to turn your functors
into parameterized classes: then you can specify constraints on the
parameters with #-types.
class ['a] r = object
constraint 'a = #o
val mutable l : 'a list = []
method register o = l <- o :: l
method react = List.iter (fun o -> o#react) l
end
class ['a] r' = object
constraint 'a = #o'
inherit ['a] r
method render = List.iter (fun o -> o#render) l
end
But I don't know what you have precisely in mind.
Jacques
-------------------
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
next prev parent reply other threads:[~2004-02-04 2:57 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-02-03 18:06 Damien
2004-02-03 19:37 ` Matt Gushee
2004-02-03 19:39 ` [Caml-list] functors and objects' Matt Gushee
2004-02-03 20:26 ` Damien Pous
2004-02-04 2:57 ` Jacques Garrigue [this message]
2004-02-05 8:55 ` [Caml-list] functors and objects Damien
2004-02-05 9:18 ` Jacques Garrigue
2004-02-05 13:45 ` Damien
2004-02-06 1:30 ` Jacques Garrigue
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=20040204115745J.garrigue@kurims.kyoto-u.ac.jp \
--to=garrigue@kurims.kyoto-u.ac.jp \
--cc=Damien.Pous@ens-lyon.fr \
--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