Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: Non-destructive record update??
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Pierre Weis <weis@p...>
Subject: Re: Non-destructive record update??
> The syntax could be
> 
> *   x.y <- 2 (may be confusing with mutable syntax ?)
> 
> *   let y = 2 in record x 
> 
> Or anything else, but there really should be something !

We propose the notation {expr with label1 = e1; lable2 = e2 ... } to
mean the record returned by the expression expr with fields label1,
label2, ... set to values e1, e2, ...

This is reminiscent of the usual (functionnal) notation for records,
and does not confuse with field update.

As an example, you may define functionnal translations for points as:

type point = {x : float; y : float; color : int};;

let translate q = {q with x = q.x +. 1.0};;

As an additional benefit, this notation provides a simple way to
define default values for records fields. For example:

let default_point = {x = 0.0; y = 0.0; color = black};;

let p = {default_point with x = 2.0};;

> With the second syntax it is easy to extend the parser, 
> and you could have a "and" to update more than one field, with only one copy
> of the original record:
> 
>     let y = 2 and z = 3 in record x
> 
>  This is not the case with the straight forward compilation of:
> 
>     (x.y <- 2).z <- 3

This new way of defining records is also straightforward to compile.

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/