What is the use of braces in Haskell?

Code below

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) 

is the source code of the GHC. I want to know why the author of this code uses curly braces instead of arguments. And how the variables haCharBuffer, haBuffers, haBufferMode take values โ€‹โ€‹from ref, spare_ref and mode. These values โ€‹โ€‹are not defined. The code file is ghc-7.4.1 \ libraries \ base \ GHC \ IO \ Handle \ Text.hs

Another piece of code from the GHC that needs clarification is the following:

 flushByteWriteBuffer :: Handle__ -> IO () flushByteWriteBuffer h_@Handle __{..} = do bbuf <- readIORef haByteBuffer when (not (isEmptyBuffer bbuf)) $ do bbuf' <- Buffered.flushWriteBuffer haDevice bbuf writeIORef haByteBuffer bbuf' 

In the ghc-7.4.1 code file \ libraries \ base \ GHC \ IO \ Handle \ Internals.hs What is the use of dots inside curly braces?

thanks

+4
source share
2 answers

The Handle__ data Handle__ was probably defined with the syntax of the entry, for example:

 data Handle__ = Handle__ { haCharBuffer :: IORef (...something...) , haBuffers :: IORef (...something...) , haBufferMode :: BufferMode } 

Curly brackets use matches with record type fields. So the statement says: "Check if the argument Handle__ constructor, in this case, save the haCharBuffer value in ref , the haBuffers value in spare_ref and the haBufferMode in mode value"

When you write Handle__ {..} , this is the same as saying Handle__ { haCharBuffer = haCharBuffer, haBuffers = haBuffers, haBufferMode = haBufferMode } ; all fields in the data structure are tied to their field names.

+12
source

The syntax of record types uses curly braces. In this code, pattern matching is used to deconstruct a record type argument in its component fields.

+4
source

All Articles