From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id A2231BC69 for ; Thu, 23 Aug 2007 00:20:01 +0200 (CEST) Received: from anchor-post-30.mail.demon.net (anchor-post-30.mail.demon.net [194.217.242.88]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l7MMK16i009648 for ; Thu, 23 Aug 2007 00:20:01 +0200 Received: from orion.metastack.com ([80.177.38.218]) by anchor-post-30.mail.demon.net with esmtp (Exim 4.42) id 1INyYS-000Io3-39 for caml-list@yquem.inria.fr; Wed, 22 Aug 2007 22:20:01 +0000 Received: from treble (cpc2-cmbg6-0-0-cust535.cmbg.cable.ntl.com [81.107.34.24]) (authenticated bits=0) by orion.metastack.com (8.13.4/8.13.3) with ESMTP id l7MM4ZCu001865 (version=TLSv1/SSLv3 cipher=RC4-MD5 bits=128 verify=NO) for ; Wed, 22 Aug 2007 23:04:36 +0100 From: "David Allsopp" To: References: <72195753-8535-4D6F-BD60-79ED3C21B5D9@research.att.com> Subject: RE: [Caml-list] safe casting Date: Wed, 22 Aug 2007 23:20:01 +0100 Organization: MetaStack Solutions Ltd. Message-ID: <00b501c7e50a$958abe40$6a7ba8c0@treble> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook 11 In-Reply-To: <72195753-8535-4D6F-BD60-79ED3C21B5D9@research.att.com> X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3138 thread-index: Acfk6YfJYIW+l0K+SPuWXQeNfIgbvgAH6o0A X-Miltered: at discorde with ID 46CCB691.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; parametric:01 runtime:01 checker:01 runtime:01 o'caml:01 interfacing:01 caml-list:01 functions:01 int:01 int:01 data:02 essentially:02 supported:02 labels:03 labels:03 > Given two record types that are identical but in name, is it safe (if > perhaps a poor idea) to cast between functions that are parametric in > these record types: Yes, it's safe because the representations of the data are the same - Obj.magic essentially removes all the protection of the type system by giving you an 'a -> 'b function. And, yes, it's a bad idea unless you have a *really* good reason to do it and a lot of time to comment your code ;o) ... > Put another way: is the implementation of two records w/ identical labels > the same, or is there a runtime significance to their static difference? The labels of a record are only of concern to the type checker - they are discarded at runtime. Two records will have the same runtime representation iff the types of each member (reading left -> right) are the same. e.g. type t = {a : int; b : int} and type u = {c : int; d : int} are the same at runtime. I *think* you can also get away with: type t = {a : int; b : int} type u = {c : int; d : int; e : int} (Obj.magic {c = 3; d = 4; e = 5} : t) i.e. a "downcast" of a variable of type u to one of type t: but I'm not sure that would "work" in all contexts (especially where copying may be concerned). However, it's all terribly bad programming practice in O'Caml because you end up with code that is totally resistant to change and that relies on the Obj module (which is not officially supported). Chapter 18 (Interfacing with C) of the manual helps with understanding the underlying representations of values at runtime. HTH, David