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 PAA25698; Fri, 31 May 2002 15:59:00 +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 PAA25658 for ; Fri, 31 May 2002 15:59:00 +0200 (MET DST) Received: from favie.faith.gr.jp (favie.faith.gr.jp [61.127.175.250]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g4VDww515895 for ; Fri, 31 May 2002 15:58:58 +0200 (MET DST) Received: from localhost (dhcp7.faith.gr.jp [192.168.1.17]) by favie.faith.gr.jp (8.9.3/8.9.3) with ESMTP id WAA11230; Fri, 31 May 2002 22:58:47 +0900 To: warplayer@free.fr Cc: caml-list@inria.fr Subject: Re: [Caml-list] about Obj.magic In-Reply-To: <003601c20884$0d78efd0$7900a8c0@warp> References: <003601c20884$0d78efd0$7900a8c0@warp> X-Mailer: Mew version 1.94.2 on Emacs 21.2 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-Id: <20020531225840C.garrigue@kurims.kyoto-u.ac.jp> Date: Fri, 31 May 2002 22:58:40 +0900 From: Jacques Garrigue X-Dispatcher: imput version 20000228(IM140) Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk From: "Warp" > > So, we should be very careful to use 'Obj.magic'. Can somebody tell me > > the appropriate situation to use this function? > > One case can be 'almost-legal' : downcasting an object to its implementation > class if you're sure that it has been previously upcasted to the interface > type class. > > (* MLI file *) > > class type intf = > sig > method a : unit > end > > val new_intf : intf -> intf > > (* ML file *) > > class impl parent = > object > method a = .... > method b = .... > end > > let new_intf i = (new impl (Obj.magic i : impl) :> intf) (* upcast impl to > intf in order to hide method 'b' *) 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. Jacques Garrigue ------------------- 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