From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id QAA26184; Fri, 31 May 2002 16:08:39 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id QAA26182 for ; Fri, 31 May 2002 16:08:38 +0200 (MET DST) X-SPAM-Warning: Sending machine is listed in blackholes.five-ten-sg.com Received: from mel-rto6.wanadoo.fr (smtp-out-6.wanadoo.fr [193.252.19.25]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g4VE8c516537 for ; Fri, 31 May 2002 16:08:38 +0200 (MET DST) Received: from mel-rta7.wanadoo.fr (193.252.19.61) by mel-rto6.wanadoo.fr (6.5.007) id 3CF4A08C001912F5 for caml-list@inria.fr; Fri, 31 May 2002 16:08:37 +0200 Received: from debian (80.8.81.26) by mel-rta7.wanadoo.fr (6.5.007) id 3CEE0FF4003FF62D for caml-list@inria.fr; Fri, 31 May 2002 16:08:37 +0200 Received: from moi by debian with local (Exim 3.35 #1 (Debian)) id 17Dn6f-0000rY-00 for ; Fri, 31 May 2002 16:10:17 +0200 To: caml-list@inria.fr Subject: Re: [Caml-list] about Obj.magic References: <003601c20884$0d78efd0$7900a8c0@warp> <20020531225840C.garrigue@kurims.kyoto-u.ac.jp> From: Remi VANICAT Date: Fri, 31 May 2002 16:10:17 +0200 In-Reply-To: <20020531225840C.garrigue@kurims.kyoto-u.ac.jp> (Jacques Garrigue's message of "Fri, 31 May 2002 22:58:40 +0900") Message-ID: <87lma0v25y.dlv@wanadoo.fr> User-Agent: Gnus/5.090006 (Oort Gnus v0.06) Emacs/21.2 (i386-debian-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-15 Content-Transfer-Encoding: 8bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Jacques Garrigue writes: > From: "Warp" > > And here is how to break your code: > > class c = object method a : unit end > let i = new_intf (new c) > > No, this is not legal in any way. > You shouldn't use Obj.magic, except if you have a formal proof that it > is correct! This supposes a deep knowledge of the type system. > > By the way, here is a simple way to do downcasts, when really needed. > > # let tbl : (< >, impl) Hashtbl.t = Hashtbl.create 17 > let register obj = Hashtbl.add tbl (obj :> < >) obj > let recover obj = Hashtbl.find tbl (obj :> < >) ;; > val tbl : (< >, impl) Hashtbl.t = > val register : impl -> unit = > val recover : < .. > -> impl = > > If you register all objects of class impl, then you can use recover to > get back their original type. If the class is not impl, you will get > an exception rather than a segmentation fault. No need to do anything > unsafe. This is implemented in my hweak library, but using weak Hashtable (and so an impl object will be freed if there is not any other things that point to them). (http://aspellfr.free.fr/) -- Rémi Vanicat vanicat@labri.u-bordeaux.fr http://dept-info.labri.u-bordeaux.fr/~vanicat ------------------- 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