From: Fabrice Le Fessant <fessant@pa.dec.cmo>
To: caml-list@inria.fr
Subject: Objects contrib
Date: Tue, 15 Jun 1999 14:45:11 -0700 (PDT) [thread overview]
Message-ID: <14182.51559.452813.300967@virtualc5.pa.dec.com> (raw)
I have made a small patch to ocaml-2.02 to allow safe casts of objects.
The patch adds two new keywords "implements" and "cast".
- "implements" ("implements c1 c2") is used to declare that objects
from a class c1 can be cast to the type of another class
c2. Implements checks at compile time that a such cast is
safe. Polymorphic classes (class ['a] ...) are not allowed as
"implements" parameters. However, derived classes are allowed
(class b = [int] t). By default, casting objects to their original class
is always allowed without using "implements", but all other casts must be
precedeed by an "implements" which allows them.
- "cast" ("cast o1 c1") is used to cast an object o1 to the type of a class c1.
A runtime check is performed to verify that the original class of the object
can be cast to the new class (thanks to an "implements" instruction). If
not correct, a (Failure "Cast failure") is raised.
Here is an example of use. Such a cast is interesting to retrieve the
original type of an object after it has been subtyped to be stored in
a generic structure.
class t1 () =
object
method a = 0
end;;
class t2 () =
object
method b = 1
method a = 0
end;;
let o1 = new t1 ();;
let o2 = new t2 ();; (* val o2 : t2 *)
let x = (o2 :> t1);; (* val x : t1 *)
let y = cast x t2;; (* cast x to its original class, val y : t2 *)
implements t2 t1;; (* t2 implements the interface of t1 *)
let x = (o2 :> < >);; (* val x: < > no methods *)
let y = cast x t1;; (* cast x to the interface of t1, val y : t1 *)
let z = cast o1 t2;; (* ERROR: Failure "Cast failure" *)
implements t1 t2;; (* ERROR: not (t1 :> t2) *)
This patch is available at:
http://pauillac.inria.fr/~lefessan/src/patch-cast.tar.gz
- Fabrice LE FESSANT
Homepage: http://pauillac.inria.fr/~lefessan
next reply other threads:[~1999-06-16 0:34 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
1999-06-15 21:45 Fabrice Le Fessant [this message]
1999-06-16 8:13 ` Vyskocil Vladimir
1999-06-16 15:56 ` Objects contrib: new URL Fabrice Le Fessant
1999-06-17 2:26 ` Jacques GARRIGUE
1999-06-17 17:16 ` Fabrice Le Fessant
1999-06-17 18:44 ` Stefan Monnier
1999-06-18 1:19 ` 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=14182.51559.452813.300967@virtualc5.pa.dec.com \
--to=fessant@pa.dec.cmo \
--cc=caml-list@inria.fr \
--cc=fessant@pa.dec.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