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
pattern matching and records vs tuples
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-04-14 (16:40)
From: Goswin von Brederlow <goswin-v-b@w...>
Subject: Re: [Caml-list] pattern matching and records vs tuples
Yoann Padioleau <padator@wanadoo.fr> writes:

> Hi, 
> I've found that while records provide advantages over tuples, 
> they also have disadvantages when it comes to evolution issues. 
> If I decide to evolve code using a tuple type, for instance adding
> new information and so extend a 4-uple in a 5-uple, then the compiler
> will tell me all the places that I need to update, which is good.
> If I use records instead, and have 4 fields, and I want to add again
> some new information in a new field, then the way the compiler works
> right now will not help me at all. E.g with this code 
>   type record = { 
>     field1: int;
>     field2: int;
>   }
>   let foo = function 
>    { field1 = v1; field2 = v2 } -> ...
> If I extend record with a new field   field3, then ocaml will
> not warn me and tell me to modify also the 'foo' function :(

On the other hand that is a verry good thing.

let set_field1 r x = { r with field1 = x }
let set_field2 r x = { r with field2 = x }

Try doing that with tuples and then add a 3rd, 4th, 5th field.

I have to say I didn't even know you could match a record partially
or have ever wanted to match a record at all. I use record only when I
have a collection of otherwise independent values. As such any
matching will be done on a single component of the record but never
pairs of them. At least I can't remember having done so before.