Imperative Object Destruction

Ashley Yakeley ashley@semantic.org
Mon, 13 Nov 2000 02:33:29 -0800


At 2000-11-13 01:27, Chris Angus wrote:

>why not create an abstract datatype 
>
>OpenFile a which is a monad 
>
>data OpenFile a = OpenFile (Maybe FileHandle -> IO a)
>
>and create you operations in terms of this 
>
>openFile :: String -> OpenFile ()
>count    :: OpenFile Int
>read	   :: Int -> OpenFile [Byte]
>
>then you could habe a run function
>
>runOF :: OpenFile a -> IO a
>
>which ran whatever you wanted for the file and ensured tahthe file was
>closed correctly afterwards.

This is a slight generalisation of my scheme, from which

  withFile :: OpenFile a -> String -> IO a
  withFile operation name = runOF (openFile name >> operation)

>How we would do this with 2 files however I'm not so sure.

Looks like you can't.

This might work, however:

  read :: (Int,Int) -> OpenFile IO [Byte]
  write :: (Int,[Byte]) -> OpenFile IO ()
  withFile :: OpenFile a -> String -> a

  copyFile :: OpenFile OpenFile IO ()

  withFile (withFile copyFile "dest") "source"

...but I'm not sure how to write copyFile.

-- 
Ashley Yakeley, Seattle WA