glynn.clements at virgin.net
Sun Oct 26 01:31:58 EDT 2003
Dominic Steinitz wrote:
> Does anyone have any views on how to "wrap" code that has been designed to
> take input from handles but you now want to take input from strings? I want
> to make minimal changes to a module that already works and to code that uses
> that module. Here's my thinking below. I'd be interested in other approaches
> and pros and cons.
> Even nearer but still not good enough. So I went for type class:
> class Monad m => Foo m where
> get' :: Handle -> m Char
> instance Foo IO where
> get' = hGetChar
> instance Foo (State String) where
> get' = \_ -> do (x:xs) <- get; put xs; return x
For general usage, you would also need an equivalent of hIsEOF.
Also, for simple applications, you could just operate on String, and
use lazy I/O for dealing with files.
Aside: it would be useful to have a more robust interface to lazy I/O,
withHandle :: Handle -> (String -> a) -> IO a
which would be roughly equivalent to:
withHandle handle f = do
string <- hGetContents handle
return $ f string
except that exceptions would be handled correctly, i.e. withHandle
would throw the exception rather than just treating it as EOF.
Glynn Clements <glynn.clements at virgin.net>
More information about the Haskell