From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=AWL autolearn=disabled version=3.1.3 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 A1735BBAF for ; Tue, 7 Apr 2009 23:32:27 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ai8GAKdj20nZSMDdZGdsb2JhbACBUpRcCwMODwW4BIN7Bg X-IronPort-AV: E=Sophos;i="4.39,339,1235948400"; d="scan'208";a="24159835" Received: from fmmailgate01.web.de ([217.72.192.221]) by mail2-smtp-roc.national.inria.fr with ESMTP; 07 Apr 2009 23:32:13 +0200 Received: from smtp05.web.de (fmsmtp05.dlan.cinetic.de [172.20.4.166]) by fmmailgate01.web.de (Postfix) with ESMTP id D6D55FF8D0C8; Tue, 7 Apr 2009 23:32:12 +0200 (CEST) Received: from [78.43.226.218] (helo=frosties.localdomain) by smtp05.web.de with asmtp (TLSv1:AES256-SHA:256) (WEB.DE 4.110 #277) id 1LrItw-0003O6-00; Tue, 07 Apr 2009 23:32:12 +0200 Received: from mrvn by frosties.localdomain with local (Exim 4.69) (envelope-from ) id 1LrItv-0000hM-U5; Tue, 07 Apr 2009 23:32:12 +0200 From: Goswin von Brederlow To: David MENTRE Cc: Goswin von Brederlow , caml-list@inria.fr Subject: Re: [Caml-list] Subtyping References: <873aclf1z8.fsf@frosties.localdomain> <3d13dcfc0904070041l5d59ec76nc97643fc862f334c@mail.gmail.com> Date: Tue, 07 Apr 2009 23:32:11 +0200 In-Reply-To: <3d13dcfc0904070041l5d59ec76nc97643fc862f334c@mail.gmail.com> (David MENTRE's message of "Tue, 7 Apr 2009 09:41:32 +0200") Message-ID: <87bpr8tajo.fsf@frosties.localdomain> User-Agent: Gnus/5.110006 (No Gnus v0.6) XEmacs/21.4.21 (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/dLXEtgezhB+H1lAs0hftgai7Epv+WOTef1X62 UM7XquFc2tHeGD843dIpXFUT4ykjifqJhIzRYI1Z9BLMkFWYRO dqMwl7i1o= X-Spam: no; 0.00; subtyping:01 ocaml:01 ocaml:01 associative:01 hashtbl:01 val:01 hashtbl:01 abstr:01 runtime:01 2009:98 48,:98 0.6:98 mfg:98 polymorphic:01 wrote:01 David MENTRE writes: > Hello, > > On Tue, Apr 7, 2009 at 07:48, Goswin von Brederlow wrote: >> In the last 2 weeks I've been playing around with lots of different >> ways to do the same thing to get a feel for what style suites me >> best. If you have improvements or alternative ways of doing the two >> things below let me know. > > Well, if you are learning OCaml, I would advise you to read regular > OCaml code, e.g. the standard library. You'll learn The Right OCaml > Style(tm). I've been using ocaml on and off for years now. Just trying out new things. >> Lets look another thing going in a similar direction. I want to >> have a structure where I can store key value pairs. But just for fun >> lets say I want to store values of different types and the key knows >> the type of value. In short a heterogeneous associative container: > > Well, why don't you just do: > > # type k = Int_k of int | Float_k of int;; > type k = Int_k of int | Float_k of int > # type v = Int_v of int | Float_v of float;; > type v = Int_v of int | Float_v of float > # let h = Hashtbl.create 3;; > val h : ('_a, '_b) Hashtbl.t = > # Hashtbl.add h (Int_k 3) (Int_v 4);; > - : unit = () > # Hashtbl.add h (Float_k 5) (Float_v 0.6);; > - : unit = () That would allow storing a float value under an int key or vice versa. Something that would completly corrupt the on-disk format of my data. One could add runtime checks that prevent this but I would really like to have this ensured by the type system at compile time. And I would like to avoid having to write a private insert functions and public let insert_int k v = insert (Int_k k) (Int_v v) let insert_float k v = insert (Float_k k) (Float_v v) For that the storing data structure would have to know that there are int and float values and I would rather have the storing structure polymorphic. MfG Goswin