* syntax bug: copying records
@ 2005-12-21 5:22 skaller
2005-12-21 6:13 ` [Caml-list] " Mackenzie Straight
0 siblings, 1 reply; 5+ messages in thread
From: skaller @ 2005-12-21 5:22 UTC (permalink / raw)
To: caml-list
If x is a record then { x } should be a copy of it. But this
doesn't work, I am using this ugly workaround instead:
{ x with dummy_field = x.dummy_field }
Surely there is a better way?
Oo.copy doesn't work either. Yes, yes, imperative programming
sucks but I'm using the standard Lexing stuff.
--
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] syntax bug: copying records
2005-12-21 5:22 syntax bug: copying records skaller
@ 2005-12-21 6:13 ` Mackenzie Straight
2005-12-21 10:41 ` Jacques Garrigue
0 siblings, 1 reply; 5+ messages in thread
From: Mackenzie Straight @ 2005-12-21 6:13 UTC (permalink / raw)
To: skaller; +Cc: caml-list
On 12/20/05, skaller <skaller@users.sourceforge.net> wrote:
> If x is a record then { x } should be a copy of it. But this
> doesn't work, I am using this ugly workaround instead:
>
> { x with dummy_field = x.dummy_field }
>
> Surely there is a better way?
Perhaps:
let copy (x:'a) = (Obj.obj (Obj.dup (Obj.repr x)) : 'a)
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] syntax bug: copying records
2005-12-21 6:13 ` [Caml-list] " Mackenzie Straight
@ 2005-12-21 10:41 ` Jacques Garrigue
2005-12-21 11:00 ` skaller
2005-12-21 18:07 ` Mackenzie Straight
0 siblings, 2 replies; 5+ messages in thread
From: Jacques Garrigue @ 2005-12-21 10:41 UTC (permalink / raw)
To: eizneckam; +Cc: skaller, caml-list
From: Mackenzie Straight <eizneckam@gmail.com>
> On 12/20/05, skaller <skaller@users.sourceforge.net> wrote:
> > If x is a record then { x } should be a copy of it. But this
> > doesn't work, I am using this ugly workaround instead:
> >
> > { x with dummy_field = x.dummy_field }
> >
> > Surely there is a better way?
>
> Perhaps:
>
> let copy (x:'a) = (Obj.obj (Obj.dup (Obj.repr x)) : 'a)
Not a very good idea:
# copy 3;;
Segmentation fault
This also explains why you need at least one record field in the
"with" notation, otherwise there is no way to know for sure the type
of the record you're copying.
Jacques Garrigue
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] syntax bug: copying records
2005-12-21 10:41 ` Jacques Garrigue
@ 2005-12-21 11:00 ` skaller
2005-12-21 18:07 ` Mackenzie Straight
1 sibling, 0 replies; 5+ messages in thread
From: skaller @ 2005-12-21 11:00 UTC (permalink / raw)
To: Jacques Garrigue; +Cc: eizneckam, caml-list
On Wed, 2005-12-21 at 19:41 +0900, Jacques Garrigue wrote:
> From: Mackenzie Straight <eizneckam@gmail.com>
> > On 12/20/05, skaller <skaller@users.sourceforge.net> wrote:
> > > If x is a record then { x } should be a copy of it. But this
> > > doesn't work, I am using this ugly workaround instead:
> > >
> > > { x with dummy_field = x.dummy_field }
> > >
> > > Surely there is a better way?
> This also explains why you need at least one record field in the
> "with" notation, otherwise there is no way to know for sure the type
> of the record you're copying.
Why not?
If I write
let x = expr in ...
then the type of x is determined by the type of expr unified
with the uses of x, right?
So the same inference doesn't apply to
let x = { expr } in ..
instead, { .. } is required to have a definite type?
Couldn't this be checked instead, so that one could write:
let x = { expr : thetype } in
which IMHO is preferable to picking one of the fields,
since the latter is fragile wrt the names of the fields.
How would you copy Lexing.lexbuf .. whose contents
could be construed as implementation dependent?
Or worse .. a private record type .. which you *cannot*
copy with functional update .. although perhaps one could
argue if there is no copy constructor given it shouldn't
be copyable .. hmm.
--
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] syntax bug: copying records
2005-12-21 10:41 ` Jacques Garrigue
2005-12-21 11:00 ` skaller
@ 2005-12-21 18:07 ` Mackenzie Straight
1 sibling, 0 replies; 5+ messages in thread
From: Mackenzie Straight @ 2005-12-21 18:07 UTC (permalink / raw)
To: Jacques Garrigue; +Cc: skaller, caml-list
On 12/21/05, Jacques Garrigue <garrigue@math.nagoya-u.ac.jp> wrote:
> # copy 3;;
> Segmentation fault
I know. You can check the tag first if you really want. Like this:
let copy x =
let r = Obj.repr x in
if Obj.is_block r then
Obj.obj (Obj.dup r)
else
x
Of course, we all know that using Obj is equivalent to using a dirty
syringe you found on the sidewalk, so... Danger!
> This also explains why you need at least one record field in the
> "with" notation, otherwise there is no way to know for sure the type
> of the record you're copying.
True. Well, you don't actually need to know the type (indeed, the
runtime system needs to have enough information to copy values). But I
imagine it could have disastrous consequences when copying abstract
values anyway.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2005-12-21 18:07 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-12-21 5:22 syntax bug: copying records skaller
2005-12-21 6:13 ` [Caml-list] " Mackenzie Straight
2005-12-21 10:41 ` Jacques Garrigue
2005-12-21 11:00 ` skaller
2005-12-21 18:07 ` Mackenzie Straight
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox