Skip to content

(Compositional) function mkWeakChan

Following the existing functions mkWeakIORef and mkWeakMVar, I'd like to have a function mkWeakChan with the following type signature, similar to that of mkWeakMVar requested by ticket #7285:

mkWeakChan :: Chan a -> v -> Maybe (IO ()) -> IO (Weak v)

The implementation of Chan is private, so it is not easily possible to implement it yourself. The implementation of both functions may look like the following:

mkWeakMVar :: MVar a -> v -> Maybe (IO ()) -> IO (Weak v)
mkWeakMVar (MVar m#) v (Just f) = IO $ \s ->
  case mkWeak# m# v f s of (# s1, w #) -> (# s1, Weak w #)
mkWeakMVar (MVar m#) v Nothing  = IO $ \s ->
  case mkWeakNoFinalizer# m# v s of { (# s1, w #) -> (# s1, Weak w #) }

mkWeakChan :: Chan a -> v -> Maybe (IO ()) -> IO (Weak v)
mkWeakChan c@(Chan readVar writeVar) v f = do
  _ <- mkWeakMVar readVar  writeVar Nothing
  mkWeakMVar writeVar v f
Trac metadata
Trac field Value
Version 7.6.3
Type FeatureRequest
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component libraries/base
Test case
Differential revisions
BlockedBy
Related #7285
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information