Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] anonymous record copy?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2001-06-28 (02:48)
From: Chris Hecker <checker@d...>
Subject: [Caml-list] anonymous record copy?

How do you copy a record without a) updating anything in the record,
and b) not knowing the type of the record?

type foo = { mutable bar : int }

let a = { bar = 12 }

let copy_record = ???  

(* hopefully copy_record is 'a -> 'a, not foo -> foo *)

let b = copy_record a <- 234  (* should not modify *)

There is Obj.dup, but it's in, so that's bad, right?  There
doesn't seem to be a way to use the "with" update syntax in a
polymorphic and generic way, and you always have to specify a field
name to update anyway.  For any individual type, I could just reassign
all the values (or update a single field), but that's really bad mojo
when you're changing your records during development.  There's also
Oo.copy, but that doesn't work on records.

Any ideas, or rationale of why this isn't possible?


Bug reports:  FAQ:
To unsubscribe, mail  Archives: