English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-04-07 (07:41)
From: David MENTRE <dmentre@l...>
Subject: Re: [Caml-list] Subtyping

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

> 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 = ()