[Haskell-cafe] Functor instance for weak pointers?

David Roundy droundy at abridgegame.org
Sun Aug 29 13:40:20 EDT 2004

Might it be possible to create a Functor instance for weak pointers?

I don't really need to, but it would certainly be handy.  Unfortunately, I
don't see how it could be implemented, since there's no way to create a new
already-tombstoned weak pointer, and without that ability, I can't see how
one could do this.

I'd like to write

weakFmap :: (a -> b) -> Weak a -> Weak b

but don't see how I can, since if deRefWeak returns Nothing, I have no way
of creating a Weak b that will also return Nothing.  :( A simple

emptyWeak :: Weak a

would be sufficient, as I could then write

weakFmap f wa = unsafePerformIO $ do ma <- deRefWeak wa
                                     case ma of
                                       Nothing -> return emptyWeak
                                       Just b -> mkWeakPtr (f b) Nothing
David Roundy

More information about the Haskell-Cafe mailing list