Re: Imperative Object Destruction
At 2000-11-13 02:18, Hannah Schroeter wrote:
That's the problem. And I think your solution is overly complicated.
Why not copy what Common Lisp does, just that Haskell can do it without macros:
withOpenFile :: FilePath -> IOMode -> (Handle -> IO a) -> IO a withOpenFile name mode action = do handle <- openFile name mode result <- (action handle) `finally` (hClose handle) return result
Usage:
count :: Handle -> IO Int read :: Handle -> IO [Byte]
fileLength <- withOpenFile "filename" ReadMode $ \handle -> count handle fileContent <- withOpenFile "filename" ReadMode $ \handle -> read handle
Doesn't fulfill condition 2: 2. no read or write operations are performed on file-handles that have not yet been opened, or that have already been closed. ...since you can do stealHandle = withOpenFile "filename" ReadMode (\handle -> handle) stealHandle >>= read -- Ashley Yakeley, Seattle WA
Hello! On Mon, Nov 13, 2000 at 02:30:17AM -0800, Ashley Yakeley wrote:
[...]
Doesn't fulfill condition 2:
2. no read or write operations are performed on file-handles that have not yet been opened, or that have already been closed.
...since you can do
stealHandle = withOpenFile "filename" ReadMode (\handle -> handle) stealHandle >>= read
And that yields an exception, as the handle is definitely closed. Is there any *real* problem with this? Kind regards, Hannah.
participants (2)
-
Ashley Yakeley -
Hannah Schroeter