caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Richard Jones <rich@annexia.org>
To: Christopher L Conway <cconway@cs.nyu.edu>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Immutable cyclic data structures via a
Date: Sun, 14 Dec 2008 20:06:20 +0000	[thread overview]
Message-ID: <20081214200620.GA11368@annexia.org> (raw)
In-Reply-To: <loom.20081213T213803-424@post.gmane.org>

On Sat, Dec 13, 2008 at 09:45:07PM +0000, Christopher L Conway wrote:
> This makes me wonder: if you add mutable fields to a type in order to
> build a cyclic data structure, but you intend to use the data
> structure immutably once it is built, is there any way to "freeze" the
> value, producing an immutable cyclic structure? This is a common
> pattern, similar to Bloch's Builder pattern for Java.[1]

Note that extlib does this in the ExtList.List module, using lots of
Obj.magic of course:

http://code.google.com/p/ocaml-extlib/source/browse/trunk/extlib/extList.ml#31

This is purely for speed, so we can do things like appending to
(private) lists when building them, and returning a real list.

IIRC it was rather controversial at the time, and there is still some
question as to whether it is really safe -- doesn't the compiler track
mutable objects specially so the introduction of major heap -> minor
heap pointers (ie. old -> new objects) works correctly with the
garbage collector?

In any case, it seems to work.  I have used ExtList.List extensively
in masses of production code and never found a problem.

Rich.

-- 
Richard Jones
Red Hat


      parent reply	other threads:[~2008-12-14 20:06 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-12-13 21:45 Christopher L Conway
2008-12-13 23:30 ` [Caml-list] " Jeremy Yallop
2008-12-13 23:43   ` Jeremy Yallop
2008-12-14  1:47 ` Edgar Friendly
2008-12-14  9:10   ` Francois Pottier
2008-12-14 14:29     ` blue storm
2008-12-14 20:06 ` Richard Jones [this message]

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=20081214200620.GA11368@annexia.org \
    --to=rich@annexia.org \
    --cc=caml-list@inria.fr \
    --cc=cconway@cs.nyu.edu \
    /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).