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 E480BBBAF for ; Sun, 20 Dec 2009 19:17:27 +0100 (CET) X-IronPort-AV: E=Sophos;i="4.47,428,1257116400"; d="scan'208";a="39080489" Received: from peray.inria.fr (HELO localhost) ([128.93.8.98]) by mail2-relais-roc.national.inria.fr with ESMTP; 20 Dec 2009 19:17:27 +0100 Content-Type: text/plain; charset=UTF-8 Cc: caml-list caml-list Subject: Re: [Caml-list] obj.magic for polymorphic record fields From: Nicolas Pouillard To: Jacques Le Normand In-reply-to: References: Date: Sun, 20 Dec 2009 19:17:27 +0100 Message-Id: <1261332875-sup-5067@peray> User-Agent: Sup/git Content-Transfer-Encoding: 8bit X-Spam: no; 0.00; ocaml-list:01 foo:01 baz:01 sig:01 val:01 struct:01 baz:01 2009:98 polymorphic:01 polymorphic:01 caml-list:01 int:01 int:01 modules:02 module:03 Excerpts from Jacques Le Normand's message of Sun Dec 20 18:44:57 +0100 2009: > Dear ocaml-list, > the following code does not type check: > > type foo = {bar : 'a. 'a -> 'a} > let a : int -> int = fun x -> x > let baz = {bar = Obj.magic a} > > with the error > > Error: This field value has type 'a -> 'a which is less general than > 'b. 'b -> 'b > > > my question is: how could I use obj.magic to get a polymorphic type? Apart from the traditional "You should avoid Obj.magic as much as possible!" I once faced this situation and the solution is to use modules. module M : sig val f : 'a -> 'a end = struct let f = Obj.magic end ;; let baz = {bar = M.f };; Have fun! -- Nicolas Pouillard http://nicolaspouillard.fr