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 mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id 5638BBBAF for ; Sun, 20 Dec 2009 19:46:00 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AvwDALb/LUtQDPKYfGdsb2JhbACECowMCYszAQELCQoHEwWnV442gS+CLVIEhlg X-IronPort-AV: E=Sophos;i="4.47,428,1257116400"; d="scan'208";a="52558868" Received: from smtp2f.orange.fr ([80.12.242.152]) by mail4-smtp-sop.national.inria.fr with ESMTP; 20 Dec 2009 19:46:00 +0100 Received: from me-wanadoo.net (localhost [127.0.0.1]) by mwinf2f26.orange.fr (SMTP Server) with ESMTP id 4CE7B80002ED for ; Sun, 20 Dec 2009 19:45:59 +0100 (CET) Received: from me-wanadoo.net (localhost [127.0.0.1]) by mwinf2f26.orange.fr (SMTP Server) with ESMTP id 3F24C8000432 for ; Sun, 20 Dec 2009 19:45:59 +0100 (CET) Received: from Lenovo-R60 (Mix-Lyon-303-4-106.w193-248.abo.wanadoo.fr [193.248.106.106]) by mwinf2f26.orange.fr (SMTP Server) with ESMTP id 7CF8080002ED for ; Sun, 20 Dec 2009 19:45:58 +0100 (CET) X-ME-UUID: 20091220184558511.7CF8080002ED@mwinf2f26.orange.fr X-ME-User-Auth: alphablock From: "Damien Guichard" Subject: Re[2]: [Caml-list] obj.magic for polymorphic record fields Cc: "caml-list caml-list" Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 Date: Sun, 20 Dec 2009 19:45:53 +0100 Message-ID: <531142069376641352@orange.fr> X-Mailer: EssentialPIM Portable v. 3.12 X-Spam: no; 0.00; damien:01 guichard:01 foo:01 baz:01 damien:01 ocaml-list:01 foo:01 baz:01 sig:01 val:01 struct:01 beginner's:01 ocaml:01 bug:01 2009:98 > 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} However it issues a warning so i acknowledge it's less elegant. - damien Le 20/12/2009 à 19:17:27, Nicolas Pouillard à écrit : > >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 > >_______________________________________________ >Caml-list mailing list. Subscription management: >http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list >Archives: http://caml.inria.fr >Beginner's list: http://groups.yahoo.com/group/ocaml_beginners >Bug reports: http://caml.inria.fr/bin/caml-bugs -- Mail created using EssentialPIM Free - www.essentialpim.com