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 mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id 871907FA6D for ; Thu, 24 Apr 2014 17:30:26 +0200 (CEST) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of gdsfh1@gmail.com) identity=pra; client-ip=209.85.223.182; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="gdsfh1@gmail.com"; x-sender="gdsfh1@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of gdsfh1@gmail.com designates 209.85.223.182 as permitted sender) identity=mailfrom; client-ip=209.85.223.182; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="gdsfh1@gmail.com"; x-sender="gdsfh1@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-ie0-f182.google.com) identity=helo; client-ip=209.85.223.182; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="gdsfh1@gmail.com"; x-sender="postmaster@mail-ie0-f182.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AhQCAC4tWVPRVd+2lWdsb2JhbABZg1VXgw+nOZlugRMIFg4BAQEBBw0JCRIqgiYBBSMEGQEbHgMMBgULAwwCJgICIgERAQUBHAYbiCQBAxGcOIwQUYMNmAAKGScNZIVyEQEBBAyBHYszggOCb4FKAQOJX48WkGUYKYRkOg X-IPAS-Result: AhQCAC4tWVPRVd+2lWdsb2JhbABZg1VXgw+nOZlugRMIFg4BAQEBBw0JCRIqgiYBBSMEGQEbHgMMBgULAwwCJgICIgERAQUBHAYbiCQBAxGcOIwQUYMNmAAKGScNZIVyEQEBBAyBHYszggOCb4FKAQOJX48WkGUYKYRkOg X-IronPort-AV: E=Sophos;i="4.97,919,1389740400"; d="scan'208";a="70089641" Received: from mail-ie0-f182.google.com ([209.85.223.182]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 24 Apr 2014 17:30:25 +0200 Received: by mail-ie0-f182.google.com with SMTP id at1so1271589iec.13 for ; Thu, 24 Apr 2014 08:30:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=EiH4GaqfYDcAAZTb5Ke8CzJa95O5c5/J3g2Fps/Y9pI=; b=NANTCDXex9X6E8bsSVckDAUaSwL7oA/VZrNTQCj8KVw8kXkG98QDY5KNWr7lMWBMBe mg7a3gH1IB0EIvlwIxYYbc1alQHRH2ysCk9aRwIs36dqLM50Y75NHySm0cgbEFaiVGYn l+Bc5A8UkL2KmA/Js8HahAWB9ZeCSPhHyY7y46xzaZBaO2ThWdo7rqPHtWflX0hOVp0u fOXcGx+FDvICDhdO2ezr2/BcFMHbIMENyn2x0Qen/SbxvFEoZ10xcWFRVAk1ANun+hzJ EFJVIOicBHTG8bJ3YJNCuk8g8Q09wrur5JD+qjF+onk1fIcOsCQQbzHfQqP/Sig6TA6z t7/w== MIME-Version: 1.0 X-Received: by 10.43.141.81 with SMTP id jd17mr2305565icc.39.1398353424400; Thu, 24 Apr 2014 08:30:24 -0700 (PDT) Received: by 10.43.115.70 with HTTP; Thu, 24 Apr 2014 08:30:24 -0700 (PDT) In-Reply-To: <5356225B.1090305@cryptosense.com> References: <5356225B.1090305@cryptosense.com> Date: Thu, 24 Apr 2014 18:30:24 +0300 Message-ID: From: Dmitry Grebeniuk To: Romain Bardou Content-Type: text/plain; charset=UTF-8 Subject: Re: [Caml-list] Obj.magic for polymorphic identifiers 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 = }] # 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 "";; val dump : Cdt.ubox -> string = # List.iter (fun ub -> print_endline (dump ub)) hlist;; 123 qwe true - : unit = () # 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.