closing file handles
Wolfgang Jeltsch
wolfgang@jeltsch.net
Sun, 9 Sep 2001 23:21:06 +0200
Tom Pledger wrote:
> [...]
> go' f
> = do h <- openFile f ReadMode
> text <- hGetContents h
> case text of
> (c:_) -> do hClose h
> return c
>
> This way, the pattern match on `text' happens before the innermost
> `do', which in turn closes the handle before letting the rest of the
> computation continue.
I think the above code will lead to problems if file input is done when c is
evaluated and not earlier.
Pattern-matching against (c:_) will not evaluate c. So the hClose will AFAICS
be executed before c is evaluated. If reading is caused by an evaluation of c
taking place later the file will be already closed.
> [...]
Hal Daume wrote:
> [...]
> return (c `seq` (hClose h `seq` c))
I don't think that this causes the hClose h to be executed because evaluating
an IO expression doesn't mean to execute it. Similarily an return (hClose h)
wouldn't close any file AFAICS.
> [...]
Wolfgang