Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] "changing" immutable record elements
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Martin Jambon <martin_jambon@e...>
Subject: Re: [Caml-list] "changing" immutable record elements
On Fri, 14 May 2004, David Fox wrote:

> Unless I am missing some core language feature, it seems to me that the
> most awkward part of the language is constructing an expression that
> represents a record with a single field modified:
>
> let set_field3 rec value =
>    match rec with
>        {field1=field1; field2=field2; field3=field3; field4=field4;
> field5=field5} ->
>           {field1=field1; field2=field2; field3=value; field4=field4;
> field5=field5}
>
> is there any prettier way of doing this?

Yes!

let new_record = { old_record with field3 = value }

NB: you must specify at least one field:
let copy r = { r with some_field = r.some_field }


Martin


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners