From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id 1D35C7F8BE for ; Mon, 28 Apr 2014 09:36:29 +0200 (CEST) Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of goswin-v-b@web.de) identity=pra; client-ip=212.227.17.12; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="goswin-v-b@web.de"; x-sender="goswin-v-b@web.de"; x-conformance=sidf_compatible Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of goswin-v-b@web.de) identity=mailfrom; client-ip=212.227.17.12; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="goswin-v-b@web.de"; x-sender="goswin-v-b@web.de"; x-conformance=sidf_compatible Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mout.web.de) identity=helo; client-ip=212.227.17.12; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="goswin-v-b@web.de"; x-sender="postmaster@mout.web.de"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Aj0BACcEXlPU4xEMnGdsb2JhbABZg1W/XYVggQwWDgEBAQEBBg0JCRQogiUBAQQBJxNECwsYCSUPBSiIYAEMDMINH4cwEwSMXYElXhaDDoEVBJkLhkcSjzmBaA X-IPAS-Result: Aj0BACcEXlPU4xEMnGdsb2JhbABZg1W/XYVggQwWDgEBAQEBBg0JCRQogiUBAQQBJxNECwsYCSUPBSiIYAEMDMINH4cwEwSMXYElXhaDDoEVBJkLhkcSjzmBaA X-IronPort-AV: E=Sophos;i="4.97,942,1389740400"; d="scan'208";a="59291131" Received: from mout.web.de ([212.227.17.12]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 28 Apr 2014 09:36:28 +0200 Received: from frosties.localnet ([78.43.112.61]) by smtp.web.de (mrweb002) with ESMTPSA (Nemesis) id 0LsyBm-1X2ZqC37j0-012YEr for ; Mon, 28 Apr 2014 09:36:26 +0200 Received: from mrvn by frosties.localnet with local (Exim 4.80) (envelope-from ) id 1Weg6z-0001XX-Uo for caml-list@inria.fr; Mon, 28 Apr 2014 09:36:26 +0200 Date: Mon, 28 Apr 2014 09:36:25 +0200 From: Goswin von Brederlow To: caml-list@inria.fr Message-ID: <20140428073625.GA5776@frosties> References: <5356225B.1090305@cryptosense.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) X-Provags-ID: V03:K0:z0iXx1W51P4uxv7ziXFe9OdTX/k0Wy7pK5zMO9Mbx66rtl45NAa ZV7x9ZXQfuhJhMSp1xBnJfx8St1iwF8baW7o2SBcNU9P9OHMaj2idVSu9M/7Ne5OpS0nbLG EDk7UjNlGJpdv1+YmI5d7PF0/IFK2y2B7qVWlM5ZsOLxb3OFMB400mKsCndUMlQc33VZdki uf9CF4nOkQLqA/n1h+qBA== Subject: Re: [Caml-list] Obj.magic for polymorphic identifiers On Thu, Apr 24, 2014 at 06:30:24PM +0300, Dmitry Grebeniuk wrote: > Hello. > > It's perfectly doable without Obj at all. > I needed "semi-typed" values quite often, so > decided to write reusable library "cadastr" [1] > that handles them, along with "untyped methods", > structural [de]composition (hence [de]serialization > based on value structure) and more. > > Things you need could be written like: > > $ ocaml > Objective Caml version 3.12.1 > > # #use "topfind";; > [...] > # #require "cadastr";; > [...] > # open Cdt;; > # let hlist = [ubox ti_int 123; ubox ti_string "qwe"; ubox ti_bool true];; > val hlist : Cdt.ubox list = > [{ub_store = ; ub_uti = }; {ub_store = ; ub_uti = }; > {ub_store = ; ub_uti = }] How do you encode a type foo = Foo | Bar Someone has to implement a ti_foo for it. > # let dump ub = > let uti = ub.ub_uti in > if uti == (ti_string :> uti) then uget_exn ti_string ub else > if uti == (ti_int :> uti) then string_of_int (uget_exn ti_int ub) else > if uti == (ti_bool :> uti) then string_of_bool (uget_exn ti_bool ub) else > "";; Here you have to match every possible type, which doesn't scale. Or you limit yourself to structure, which is unsound for retrieving the original type. > val dump : Cdt.ubox -> string = > # List.iter (fun ub -> print_endline (dump ub)) hlist;; > 123 > qwe > true > - : unit = () > # So you can print the values. But how to you get them back? Where is your val unbox : type a . uti -> Cdt.ubox -> a (which isn't well typed like that) > As for "methods" I 've mentioned above, > > # List.iter (fun ub -> > let ushow = get_meth_untyped "show" ub.ub_uti in > let ures = u_app ushow ub in > print_endline (uget_exn ti_string ures) > ) hlist;; > 123 > "qwe" > True > - : unit = () > # > > However there is some runtime cost on such > "untyped" values. > > > [1] https://bitbucket.org/gds/cadastr , but it doesn't > compile with OCaml >= 4. It can be fixed; > I'll do it if someone will want to use it. MfG Goswin