From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id BFD72BBAF for ; Mon, 21 Dec 2009 14:44:59 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Aj4DAG8KL0vZSMDji2dsb2JhbACBSpoIAQEBCgsKBxEFuF6ELgQ X-IronPort-AV: E=Sophos;i="4.47,431,1257116400"; d="scan'208";a="39120921" Received: from fmmailgate02.web.de ([217.72.192.227]) by mail2-smtp-roc.national.inria.fr with ESMTP; 21 Dec 2009 14:44:59 +0100 Received: from smtp08.web.de (fmsmtp08.dlan.cinetic.de [172.20.5.216]) by fmmailgate02.web.de (Postfix) with ESMTP id 08F05149C6EFC; Mon, 21 Dec 2009 14:44:59 +0100 (CET) Received: from [95.208.117.111] (helo=frosties.localdomain) by smtp08.web.de with asmtp (TLSv1:AES256-SHA:256) (WEB.DE 4.110 #314) id 1NMiZG-0007L9-00; Mon, 21 Dec 2009 14:44:58 +0100 Received: from mrvn by frosties.localdomain with local (Exim 4.69) (envelope-from ) id 1NMiZG-0005Zp-D7; Mon, 21 Dec 2009 14:44:58 +0100 From: Goswin von Brederlow To: "Damien Guichard" Cc: "caml-list caml-list" Subject: Re: [Caml-list] obj.magic for polymorphic record fields References: <531142069376641352@orange.fr> Date: Mon, 21 Dec 2009 14:44:58 +0100 In-Reply-To: <531142069376641352@orange.fr> (Damien Guichard's message of "Sun, 20 Dec 2009 19:45:53 +0100") Message-ID: <87zl5cmof9.fsf@frosties.localdomain> User-Agent: Gnus/5.110006 (No Gnus v0.6) XEmacs/21.4.22 (linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: goswin-v-b@web.de X-Sender: goswin-v-b@web.de X-Provags-ID: V01U2FsdGVkX1/Q6v2CObHCO12Fre5hRQ4aEPKQe6s/7i0MSSQ/ 70cjQpqJmNIlMchMoITersqMVoPjc8OliZ/X65PApI3PjNbIgD ctqf+fZpM= X-Spam: no; 0.00; damien:01 guichard:01 foo:01 baz:01 damien:01 foo:01 baz:01 val:01 val:01 segfault:01 zsh:01 ocaml:01 buzz:98 buzz:98 mfg:98 "Damien Guichard" writes: >> I once faced this situation and the solution is to use modules. > That is one good practical solution. > > The simpler solution that immediatly came to my mind is eta-expansion. > > type foo = {bar : 'a. 'a -> 'a} > let a : int -> int = fun x -> x > let baz = {bar = fun x -> (Obj.magic a) x} This would create a new closure though. Wastes time and space. Bad if you create a million foo's all with the same function. > However it issues a warning so i acknowledge it's less elegant. Which I don't quite understand. > - damien Why not use magic on the overall type of the record instead of the individual function? # type foo = { foo : 'a. 'a -> 'a; } type 'a bar = {bar : 'a -> 'a} let a : int -> int = fun x -> x let baz = (Obj.magic {bar = a} : foo);; type foo = { foo : 'a. 'a -> 'a; } type 'a bar = { bar : 'a -> 'a; } val a : int -> int = val baz : foo = {foo = } It means duplicating the record type but you can magic it without warnings. Overall I have to say though that you are doing something wrong here. (Should I assume your actual use case is larger than this simple example?) # let b x = x + 1 let buzz = (Obj.magic {bar = b} : foo) buzz.foo [1;2;3];; val b : int -> int = val buzz : foo = {foo = } - : int list = [-2330612807164231680] Doesn't always segfault but lets get nastier: # (buzz.foo buzz).foo 1;; zsh: segmentation fault ocaml The obj.magic only works safely if the function you use is of type 'a -> 'a (even if the interface restricts it to a less general type). The right solution would be to loosen the interface to use the more general type. MfG Goswin