下面的代码
getSpareBuffer :: Handle__ -> IO (BufferMode, CharBuffer)
getSpareBuffer Handle__{haCharBuffer=ref,
haBuffers=spare_ref,
haBufferMode=mode}
= do
case mode of
NoBuffering -> return (mode, error "no buffer!")
_ -> do
bufs <- readIORef spare_ref
buf <- readIORef ref
case bufs of
BufferListCons b rest -> do
writeIORef spare_ref rest
return ( mode, emptyBuffer b (bufSize buf) WriteBuffer)
BufferListNil -> do
new_buf <- newCharBuffer (bufSize buf) WriteBuffer
return (mode, new_buf)
来自 GHC 源代码(ghc-7.4.1\libraries\base\GHC\IO\Handle\Text.hs)。我想知道为什么代码使用大括号代替参数。以及变量如何haCharBuffer, haBuffers, haBufferMode
从ref, spare_ref
和中获取值mode
。这些值尚未定义。
GHC 中另一个需要澄清的代码片段是:
flushByteWriteBuffer :: Handle__ -> IO ()
flushByteWriteBuffer h_@Handle__{..} = do
bbuf <- readIORef haByteBuffer
when (not (isEmptyBuffer bbuf)) $ do
bbuf' <- Buffered.flushWriteBuffer haDevice bbuf
writeIORef haByteBuffer bbuf'
在代码文件中ghc-7.4.1\libraries\base\GHC\IO\Handle\Internals.hs
大括号{..}
(