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 RAA26202 for caml-redistribution; Thu, 17 Jun 1999 17:44:33 +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 EAA22298 for ; Thu, 17 Jun 1999 04:30:06 +0200 (MET DST) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by nez-perce.inria.fr (8.8.7/8.8.7) with ESMTP id EAA24536 for ; Thu, 17 Jun 1999 04:29:45 +0200 (MET DST) Received: from localhost (safran.kurims.kyoto-u.ac.jp [130.54.16.91]) by kurims.kurims.kyoto-u.ac.jp (8.9.1a/3.7W) with ESMTP id LAA25866; Thu, 17 Jun 1999 11:26:06 +0900 (JST) To: fessant@pa.dec.com Cc: caml-list@inria.fr Subject: Re: Objects contrib: new URL ... In-Reply-To: Your message of "Wed, 16 Jun 1999 08:56:41 -0700 (PDT)" <14183.51513.8527.191081@virtualc5.pa.dec.com> References: <14183.51513.8527.191081@virtualc5.pa.dec.com> X-Mailer: Mew version 1.93 on Emacs 20.3 / Mule 4.0 (HANANOEN) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-Id: <19990617112633P.garrigue@kurims.kyoto-u.ac.jp> Date: Thu, 17 Jun 1999 11:26:33 +0900 From: Jacques GARRIGUE X-Dispatcher: imput version 980905(IM100) Content-Transfer-Encoding: 7bit Sender: weis > 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". This looks both interesting and dangerous. Interesting: Lots of people want such a feature. This is standard in LISP or JAVA, so why not in Caml? A slight improvement: I would rather have cast raise Match_failure than a standard Failure. In particular with a match failure you have position information for where it failed. Dangerous: I believe this really goes against what ML is trying to be. It allows people to write their programs like they would do in JAVA, that is in a partially unsafe way. "I believe this object will have type t, so let's cast it to t." What makes you believe so? I don't see any guarantee for that. Another improvement for that would be to use a match construct rather than only cast, and to have a warning if the match does not contain a wild card at the end. Another problem is that it creates a distinction between classes with parameters and classes without, which is not very natural. It would have been easy to introduce such a feature in the object system from the beginning: caml objects bear a pointer to their class, you just have to put the class name in the class. My guess is that it was intentionnally left out. Remark: You can already implement something similar inside the language: For each class c create an hash-table referencing the object (In fact this should not be a standard hash-table, but a set of weak pointers, otherwise your objects will never be GCed as long as you don't remove them from the table) type empty_obj = < > let memo_c : (empty_obj, c) Hashtbl.t = Hashtbl.create 7 let new_c args = let obj = new c args in Hashtbl.add memo_c (obj :> empty_obj) obj; obj let cast_c obj = Hashtbl.find memo_c (obj :> empty_obj) Do not forget to catch Not_found when using cast_c ! This works for parameterized classes also: you just have to create a different memo for each parameters. You don't have the flexibility of the implements feature, but you could also implement it by defining cast_c to look into the memos of all subclasses in the implement hierarchy. Regards, Jacques --------------------------------------------------------------------------- Jacques Garrigue Kyoto University garrigue at kurims.kyoto-u.ac.jp JG