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 LAA20179; Fri, 31 May 2002 11:19:35 +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 LAA20422 for ; Fri, 31 May 2002 11:19:34 +0200 (MET DST) Received: from relay-1m.club-internet.fr (relay-1m.club-internet.fr [194.158.104.40]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g4V9JY527997 for ; Fri, 31 May 2002 11:19:34 +0200 (MET DST) Received: from warp (lns11v-11-189.w.club-internet.fr [213.44.170.189]) by relay-1m.club-internet.fr (Postfix) with SMTP id B2B6616C8; Fri, 31 May 2002 11:19:32 +0200 (CEST) Message-ID: <003601c20884$0d78efd0$7900a8c0@warp> From: "Warp" To: "dengping zhu" , "Ocaml" References: Subject: Re: [Caml-list] about Obj.magic Date: Fri, 31 May 2002 11:17:56 +0200 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 5.50.4522.1200 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > When should I use 'Obj.magic'? It is really confusing. Now I am converting > a bunch of code from sml to ocaml. During the process, I want to make > sure that the type of every function keep consistent with the original > one. But I still encountered a lot of problems about the types in ocaml. > For examples, all my base functions are fine in ocaml. But, sometimes when > I tried to combine a few base functions together in the same way as my > original code, I got a type error. After endless trial, I have to use > 'Obj.magic'. In some sense, it is good because I can compile my code > now. On the other hand, things will be worse because it is very difficult > to debug if I use a few 'Obj.magic' in my code. I couldn't find out the > origins of all the runtime errors. > > 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. ex : (* 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' *) Nicolas Cannasse www.motion-twin.com ------------------- 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