logo       

RE: Imperative Object Destruction: msg#00042

lang.haskell.general

Subject: RE: Imperative Object Destruction

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





<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise