caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: colanderman@gmail.com
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Shallow copy of a record
Date: Sat, 22 Jan 2005 15:49:09 +0900 (JST)	[thread overview]
Message-ID: <20050122.154909.132744958.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <875c7e0705012120277c090ec8@mail.gmail.com>

From: Chris King <colanderman@gmail.com>

> Maybe I missed it in the docs, but is there a way to create a shallow
> copy of a record without modifying any of its contents?  I know
> there's the {foo with a=b} construct, but this only works with at
> least one assignment; I want to create an unmodified copy of the
> record (akin to the {< >} construct for objects).

This does not exist, and with good reason: there is no way in the type
system to define a function which works on all records, but only
records. So to do the copy you need to know at least one of the labels
of the record, which in turn gives you its type.

Another approach, which is potentially unsafe, is to use the Obj.dup
function. You can make it a bit safer by ensuring (dynamically) that
you only copy caml blocks or values:

  let copy (v : 'a) : 'a =
    let obj = Obj.repr v in
    if Obj.is_int obj then v
    else if Obj.is_block obj && let tag = Obj.tag obj in
            tag < Obj.lazy_tag || tag = Obj.double_array_tag
    then Obj.obj (Obj.dup obj)
    else invalid_arg "shallow copy"

Jacques Garrigue


  reply	other threads:[~2005-01-22  6:50 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-01-22  4:27 Chris King
2005-01-22  6:49 ` Jacques Garrigue [this message]
2005-01-22 16:55   ` [Caml-list] " Chris King
2005-01-22 17:55     ` Michal Moskal
2005-01-22 20:08       ` Chris King
2005-01-22 14:17 ` Kurt Welgehausen
2005-01-22 16:45   ` Chris King
     [not found] ` <200501221059.28064.jon@jdh30.plus.com>
2005-01-22 17:03   ` Chris King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20050122.154909.132744958.garrigue@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=colanderman@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).