caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] anonymous record copy?
@ 2001-06-28  2:48 Chris Hecker
  2001-06-28  3:42 ` Jacques Garrigue
  0 siblings, 1 reply; 2+ messages in thread
From: Chris Hecker @ 2001-06-28  2:48 UTC (permalink / raw)
  To: caml-list


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

b.bar <- 234  (* should not modify a.bar *)

There is Obj.dup, but it's in obj.ml, 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?

Chris

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] anonymous record copy?
  2001-06-28  2:48 [Caml-list] anonymous record copy? Chris Hecker
@ 2001-06-28  3:42 ` Jacques Garrigue
  0 siblings, 0 replies; 2+ messages in thread
From: Jacques Garrigue @ 2001-06-28  3:42 UTC (permalink / raw)
  To: checker; +Cc: caml-list

> 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
> 
> b.bar <- 234  (* should not modify a.bar *)

The problem is that if the type were 'a -> 'a, it would not apply only
on records, but on just anything. Constrast this with Obj.copy, which
applies on any object, but only on objects.

I believe the natural way to write the copy of a record is
{x with bar = x.bar}. You need to have at least one field, in order to
know the actual type of the record, which gives you the list of fields
to copy.

Another way would be to use the Obj module, which is implementation
dependent. The block must be marked either by a constructor tag or
double_array_tag, otherwise you can break everything. In particulars
objects have an oid, and should not be copied this way.

let copy_record (r : 'a) : 'a =
  let obj = Obj.repr r in
  if Obj.is_int obj || Obj.size obj = 0 then r else
  let tag = Obj.tag obj in
  if tag < Obj.object_tag || tag = Obj.double_array_tag then
    Obj.obj (Obj.dup obj)
  else invalid_arg "copy_record"

Cheers,

Jacques Garrigue
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2001-06-28  3:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-06-28  2:48 [Caml-list] anonymous record copy? Chris Hecker
2001-06-28  3:42 ` Jacques Garrigue

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).