caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Shallow copy of a record
@ 2005-01-22  4:27 Chris King
  2005-01-22  6:49 ` [Caml-list] " Jacques Garrigue
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Chris King @ 2005-01-22  4:27 UTC (permalink / raw)
  To: O'Caml Mailing List

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).

Thanks!


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

* Re: [Caml-list] Shallow copy of a record
  2005-01-22  4:27 Shallow copy of a record Chris King
@ 2005-01-22  6:49 ` Jacques Garrigue
  2005-01-22 16:55   ` Chris King
  2005-01-22 14:17 ` Kurt Welgehausen
       [not found] ` <200501221059.28064.jon@jdh30.plus.com>
  2 siblings, 1 reply; 8+ messages in thread
From: Jacques Garrigue @ 2005-01-22  6:49 UTC (permalink / raw)
  To: colanderman; +Cc: caml-list

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


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

* Re: [Caml-list] Shallow copy of a record
  2005-01-22  4:27 Shallow copy of a record Chris King
  2005-01-22  6:49 ` [Caml-list] " Jacques Garrigue
@ 2005-01-22 14:17 ` Kurt Welgehausen
  2005-01-22 16:45   ` Chris King
       [not found] ` <200501221059.28064.jon@jdh30.plus.com>
  2 siblings, 1 reply; 8+ messages in thread
From: Kurt Welgehausen @ 2005-01-22 14:17 UTC (permalink / raw)
  To: caml-list

It's not clear whether you require a general polymorphic
copy function or you just need to make a copy of a
particular record. In the latter case, something like

  let y = {x with a = x.a}

will do what you want (even though it's not very pretty).

Best regards


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

* Re: [Caml-list] Shallow copy of a record
  2005-01-22 14:17 ` Kurt Welgehausen
@ 2005-01-22 16:45   ` Chris King
  0 siblings, 0 replies; 8+ messages in thread
From: Chris King @ 2005-01-22 16:45 UTC (permalink / raw)
  To: Kurt Welgehausen; +Cc: caml-list

On Sat, 22 Jan 2005 08:17:19 -0600, Kurt Welgehausen <kwel@kwel.net> wrote:

> It's not clear whether you require a general polymorphic
> copy function or you just need to make a copy of a
> particular record.

Just the particular record.

> In the latter case, something like
> 
>   let y = {x with a = x.a}
> 
> will do what you want (even though it's not very pretty).

Yeah, that's what I thought of first; I was just hoping there was a
way to do it without the redundant assign (like {x} or something).


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

* Re: [Caml-list] Shallow copy of a record
  2005-01-22  6:49 ` [Caml-list] " Jacques Garrigue
@ 2005-01-22 16:55   ` Chris King
  2005-01-22 17:55     ` Michal Moskal
  0 siblings, 1 reply; 8+ messages in thread
From: Chris King @ 2005-01-22 16:55 UTC (permalink / raw)
  To: Jacques Garrigue; +Cc: caml-list

On Sat, 22 Jan 2005 15:49:09 +0900 (JST), Jacques Garrigue
<garrigue@math.nagoya-u.ac.jp> wrote:

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

Isn't it the foo in {foo with a=b} that determines the type, though?

> Another approach, which is potentially unsafe, is to use the Obj.dup
> function.

That seems like it would do the trick for now - thanks!


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

* Re: [Caml-list] Shallow copy of a record
       [not found] ` <200501221059.28064.jon@jdh30.plus.com>
@ 2005-01-22 17:03   ` Chris King
  0 siblings, 0 replies; 8+ messages in thread
From: Chris King @ 2005-01-22 17:03 UTC (permalink / raw)
  To: Jon Harrop; +Cc: O'Caml Mailing List

On Sat, 22 Jan 2005 10:59:27 +0000, Jon Harrop <jon@jdh30.plus.com> wrote:

> Why do you want to do this?

My specific need is to make a copy of the structure returned by
tcgetattr to be used to reset the terminal state (i.e. one seperate
from the "working" copy used to change the terminal state).  I realize
that I can just call tcgetattr twice (and indeed that's probably
preferable in a functional language) but my C background wants me just
to make a copy :).  (I've actually thought of other reasons not to do
this, but the problem still bugs me!)


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

* Re: [Caml-list] Shallow copy of a record
  2005-01-22 16:55   ` Chris King
@ 2005-01-22 17:55     ` Michal Moskal
  2005-01-22 20:08       ` Chris King
  0 siblings, 1 reply; 8+ messages in thread
From: Michal Moskal @ 2005-01-22 17:55 UTC (permalink / raw)
  To: Chris King; +Cc: caml-list

On Sat, 22 Jan 2005 11:55:19 -0500, Chris King <colanderman@gmail.com> wrote:
> On Sat, 22 Jan 2005 15:49:09 +0900 (JST), Jacques Garrigue
> <garrigue@math.nagoya-u.ac.jp> wrote:
> 
> > 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.
> 
> Isn't it the foo in {foo with a=b} that determines the type, though?

No, consider:

  let copy foo = {foo with a = b}

and now, if there were no assign:

  let copy foo = {foo}

what should be the type of copy?

-- 
: Michal Moskal :: http://nemerle.org/~malekith/ :: GCS !tv h e>+++ b++
: No, I will *not* fix your computer............ :: UL++++$ C++ E--- a?


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

* Re: [Caml-list] Shallow copy of a record
  2005-01-22 17:55     ` Michal Moskal
@ 2005-01-22 20:08       ` Chris King
  0 siblings, 0 replies; 8+ messages in thread
From: Chris King @ 2005-01-22 20:08 UTC (permalink / raw)
  To: Michal Moskal; +Cc: caml-list

On Sat, 22 Jan 2005 18:55:40 +0100, Michal Moskal
<michal.moskal@gmail.com> wrote:

> No, consider:
> 
>   let copy foo = {foo with a = b}
> 
> and now, if there were no assign:
> 
>   let copy foo = {foo}
> 
> what should be the type of copy?

Ah okay, I didn't think about that.  But I suppose in cases like that,
the type could be forced, as in "let copy foo = { (foo:bar) }".


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

end of thread, other threads:[~2005-01-22 20:08 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-22  4:27 Shallow copy of a record Chris King
2005-01-22  6:49 ` [Caml-list] " Jacques Garrigue
2005-01-22 16:55   ` 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

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).