[Haskell-cafe] letting go of file handles and Data.Binary

Bryan O'Sullivan bos at serpentine.com
Sun Apr 20 17:24:02 EDT 2008

Doh!  For all that I wrote about encodeFile, substitute decodeFile.
You'll need to write something to force the value that you're decoding.
 Something like this ought to do the trick.

import Data.Binary (Binary, decode)
import Control.Exception (bracket)
import qualified Data.ByteString.Lazy as L
import System.IO (IOMode(..), hClose, openFile)

strictDecodeFile :: Binary a => FilePath -> (a -> b) -> IO ()
strictDecodeFile path force =
    bracket (openFile path ReadMode) hClose $ \h -> do
      c <- L.hGetContents h
      force (decode c) `seq` return ()


More information about the Haskell-Cafe mailing list