From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id WAA12730 for caml-redistribution; Wed, 16 Jun 1999 22:56:18 +0200 (MET DST) Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id RAA15791 for ; Wed, 16 Jun 1999 17:56:54 +0200 (MET DST) Received: from mail1.digital.com (mail1.digital.com [204.123.2.50]) by nez-perce.inria.fr (8.8.7/8.8.7) with ESMTP id RAA16046 for ; Wed, 16 Jun 1999 17:56:52 +0200 (MET DST) Received: from src-mail.pa.dec.com (src-mail.pa.dec.com [16.4.16.35]) by mail1.digital.com (8.9.2/8.9.2/WV2.0g) with ESMTP id IAA09486 for ; Wed, 16 Jun 1999 08:56:50 -0700 (PDT) Received: by src-mail.pa.dec.com; id IAA27365; Wed, 16 Jun 1999 08:56:49 -0700 (PDT) Received: (from fessant@localhost) by virtualc5.pa.dec.com (8.9.3/8.9.3) id IAA09456; Wed, 16 Jun 1999 08:56:41 -0700 From: Fabrice Le Fessant Message-ID: <14183.51513.8527.191081@virtualc5.pa.dec.com> Date: Wed, 16 Jun 1999 08:56:41 -0700 (PDT) To: caml-list@inria.fr Subject: Objects contrib: new URL ... References: <14182.51559.452813.300967@virtualc5.pa.dec.com> X-Mailer: VM 6.62 under Emacs 19.34.1 Reply-To: fessant@pa.dec.com MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=iso-8859-1 Sender: weis Sorry, the previous link was false. The patch is available at: http://pauillac.inria.fr/~lefessan/src/patch.cast.gz ---------------------------------------------------------------------- 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 such a 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) *) - Fabrice Homepage: http://pauillac.inria.fr/~lefessan