In the article Grabbing an Uncomfortable Team , Simon Peyton Jones provided a "possible implementation Channel. "
type Channel a = (MVar (Stream a) , -- Read end
MVar (Stream a) ) -- Write end (the hole)
type Stream a = MVar (Item a)
data Item a = MkItem a (Stream a)
Now it implements a function putChan :: Channel a -> a -> IO ()like this
putChan (read, write) val
= do { new_hole <- newEmptyVar ;
old_hole <- takeMVar write ;
putMVar write new_hole ;
putMVar old_hole (MkItem val new_hole) }
The above function takes the MVar out of the record, then puts the empty MVar in it.
Then it writes to the old_hole extracted from the record.
The question is why is he writing old_hole? It was removed from the record, and its scope is limited only by the current block, then what's the difference?
source
share