caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: David MENTRE <dmentre@linux-france.org>
Cc: Goswin von Brederlow <goswin-v-b@web.de>, caml-list@inria.fr
Subject: Re: [Caml-list] Subtyping
Date: Tue, 07 Apr 2009 23:32:11 +0200	[thread overview]
Message-ID: <87bpr8tajo.fsf@frosties.localdomain> (raw)
In-Reply-To: <3d13dcfc0904070041l5d59ec76nc97643fc862f334c@mail.gmail.com> (David MENTRE's message of "Tue, 7 Apr 2009 09:41:32 +0200")

David MENTRE <dmentre@linux-france.org> writes:

> Hello,
>
> On Tue, Apr 7, 2009 at 07:48, Goswin von Brederlow <goswin-v-b@web.de> 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 = <abstr>
> # 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


  parent reply	other threads:[~2009-04-07 21:32 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-04-07  5:48 Subtyping Goswin von Brederlow
2009-04-07  7:41 ` [Caml-list] Subtyping David MENTRE
2009-04-07 13:39   ` Peng Zang
2009-04-07 21:33     ` Goswin von Brederlow
2009-04-07 21:32   ` Goswin von Brederlow [this message]
2009-04-08  0:38 ` Goswin von Brederlow
2009-04-08  1:35   ` Jacques Garrigue
2009-04-08  2:43     ` Jacques Garrigue
2009-04-08  5:16     ` Goswin von Brederlow

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87bpr8tajo.fsf@frosties.localdomain \
    --to=goswin-v-b@web.de \
    --cc=caml-list@inria.fr \
    --cc=dmentre@linux-france.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).