[Haskell-cafe] mkEmptyWeakPtr?

David Roundy droundy at abridgegame.org
Sat Nov 6 08:29:02 EST 2004


I was wondering whether there might be any sort of trick one could use to
create an empty "Weak a", i.e. one that has already been GCed? It should be
possible to create such a pointer without having an object of type a.

mkEmptyWeakPtr :: IO (Weak a)

I know I could get this effect by using instead a "Maybe (Weak a)", but
that's ugly and adds an additional layer of redirection.  The point is that
the weak pointer is being used to cache a value, but I'd like to not fill
the cache until I actually need to read it.

i.e. I have a data type

AntiMemo (IO a) (IORef (Weak a))

where the second term is a cache of the result of the first, which is
really a pure function, but I'm using the IO monad to force reevaluation,
so the result, which is large, can be discarded and recomputed.

So I could change this to

AntiMemo (IO a) (IORef (Maybe (Weak a)))

but it just seems a shame when Weak already effectively has a "Nothing"
value type.  I suppose one could also make it a "Weak (Maybe a)", but in
any case, I'd rather be able to just initialize the IORef to an empty weak
pointer, so there wouldn't need to be any extra code when reading the
value.
-- 
David Roundy
http://www.abridgegame.org


More information about the Haskell-Cafe mailing list