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 QAA32438; Fri, 2 Aug 2002 16:30:38 +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 QAA32428 for ; Fri, 2 Aug 2002 16:30:37 +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 g72EUR510801 for ; Fri, 2 Aug 2002 16:30:29 +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 XAA05356; Fri, 2 Aug 2002 23:29:35 +0900 To: marant.logatique@fr.thalesgroup.com Cc: caml-list@inria.fr Subject: Re: [Caml-list] Using %identity for casting types In-Reply-To: <20020802154858.A18074@fr.thalesgroup.com> References: <20020802154858.A18074@fr.thalesgroup.com> X-Mailer: Mew version 1.94.2 on Emacs 21.2 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable Message-Id: <20020802232931I.garrigue@kurims.kyoto-u.ac.jp> Date: Fri, 02 Aug 2002 23:29:31 +0900 From: Jacques Garrigue X-Dispatcher: imput version 20000228(IM140) Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk From: J=E9r=F4me Marant > > I'm doing some experiments in interfacing OCaml with C++ and > it is sometimes necessary to cast one type to another. > I've seen in lablgtk something like: > external unsafe_cast : 'a obj -> 'b obj =3D "%identity" > = > It is meant to be used by the end user? Well, not really, but if you're interfacing with C++, it's not more dangerous to do the cast in ocaml than in C++ (except if you use rtti). The above idiom is equivalent to let unsafe_cast : 'a obj -> 'b obj =3D Obj.magic It may be more efficient, as the compiler knows that unsafe_cast is the identity, and can discard it. No code is generated. I also tend to prefer it because it makes clear the need to properly write types: such casts only make sense at a precise type, while Obj.magic is 'a -> 'b, which allows too much. And, as always, only use unsafe casts for non-ocaml data. Ocaml datatypes are expressive enough that you don't need unsafe casts for them. 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