Version française
Home     About     Download     Resources     Contact us    
Browse thread
Immutable cyclic data structures via a
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Richard Jones <rich@a...>
Subject: Re: [Caml-list] Immutable cyclic data structures via a
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