From: Jerome Vouillon <Jerome.Vouillon@inria.fr>
To: Hendrik Tews <tews@irritatie.cs.kun.nl>, caml-list@inria.fr
Subject: Re: subtyping and inheritance
Date: Wed, 27 Jan 1999 15:28:31 +0100 [thread overview]
Message-ID: <19990127152831.14918@pauillac.inria.fr> (raw)
In-Reply-To: <199901201150.MAA09911@irritatie.cs.kun.nl>; from Hendrik Tews on Wed, Jan 20, 1999 at 12:50:21PM +0100
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 2380 bytes --]
On Wed, Jan 20, 1999 at 12:50:21PM +0100, Hendrik Tews wrote:
> There was already a proposal how to solve the problem by Jerome
> Vouillon. Just for demonstration I append another solution, which
> uses a self implemented type case. It supports code reuse better,
> but loses type security by using the Obj module. Maybe one of the
> people who "understand the whole source code for the O'Caml
> compiler, runtime and libraries" and by that can use the Obj
> module, can comment on its use here.
Your example indeed works, as coercions do not actually modify
objects.
When you need to make use of the Obj module, it is however advised to
limit this use to a small module with a type-safe interface. Your
example can be rewritten this way:
class top = object
inherit Property.c
method eq (x : top) = print_string "top = top ? "; true
end;;
let left_k = Property.create_key ();;
class left (l1 : int) = object (self)
inherit top as super
initializer Property.add left_k self l1
method eq x =
try
let l1' = Property.find left_k x in
print_string "left = left ? ";
l1 = l1'
with Not_found -> super#eq x
end;;
let right_k = Property.create_key ();;
class right (r1 : int) = object (self)
inherit top as super
initializer Property.add right_k self r1
method eq x =
try
let r1' = Property.find right_k x in
print_string "right = right ? ";
r1 = r1'
with Not_found -> super#eq x
end;;
-- Jérôme
type 'a t
type u and u'
class virtual s : object
method virtual properties : u
method virtual add_property : u' -> unit
end
class c : object
method properties : u
method add_property : u' -> unit
end
val create_key : unit -> 'a t
val add : 'a t -> #c -> 'a -> unit
val find : 'a t -> #c -> 'a
filename="property.ml"
type 'a t = int
module Properties = Map.Make (struct type t = int let compare = compare end)
type u' = int * Obj.t
type u = Obj.t Properties.t
class virtual s = object
method virtual properties : u
method virtual add_property : u' -> unit
end
class c = object
val mutable properties = (Properties.empty : u)
method properties = properties
method add_property (k, p) = properties <- Properties.add k p properties
end
let i = ref (-1)
let create_key () = incr i; !i
let add p x v = x#add_property (p, Obj.repr v)
let find p x = Obj.obj (Properties.find p (x#properties))
next prev parent reply other threads:[~1999-01-27 18:28 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
1999-01-11 18:52 Markus Mottl
1999-01-15 15:02 ` Jerome Vouillon
1999-01-15 17:37 ` Markus Mottl
1999-01-18 19:55 ` Jerome Vouillon
1999-01-18 21:18 ` Markus Mottl
1999-01-20 11:50 ` Hendrik Tews
1999-01-25 0:08 ` Markus Mottl
1999-01-25 15:06 ` Musings on Obj.magic (Was: subtyping and inheritance) David Monniaux
1999-01-27 14:18 ` subtyping and inheritance Jerome Vouillon
1999-01-27 14:45 ` Markus Mottl
1999-01-28 19:40 ` Hendrik Tews
1999-01-27 14:28 ` Jerome Vouillon [this message]
1999-04-15 12:18 Giuseppe Castagna
1999-04-15 16:02 ` Markus Mottl
1999-04-20 12:38 ` Didier Remy
1999-04-20 15:06 ` Giuseppe Castagna
1999-04-21 12:18 ` Didier Remy
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=19990127152831.14918@pauillac.inria.fr \
--to=jerome.vouillon@inria.fr \
--cc=caml-list@inria.fr \
--cc=tews@irritatie.cs.kun.nl \
/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