# Re: Non-destructive record update??

From: Pierre Weis (weis@pauillac.inria.fr)
Date: Thu Apr 16 1998 - 16:19:18 MET DST

From: Pierre Weis <weis@pauillac.inria.fr>
Message-Id: <199804161419.QAA16455@pauillac.inria.fr>
Subject: Re: Non-destructive record update??
To: Christophe.Raffalli@univ-savoie.fr
Date: Thu, 16 Apr 1998 16:19:18 +0200 (MET DST)
In-Reply-To: <199804251226.OAA01300@lama-d132.univ-savoie.fr> from "Christophe Raffalli" at Apr 25, 98 02:26:43 pm

> 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/

This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:14 MET