I Hate IO

Simon Marlow simonmar@microsoft.com
Thu, 16 Aug 2001 10:15:26 +0100

> I really like the idea of a source and sink class, among=20
> other things it
> would allow things like a sink which spit out a cryptographic hash of
> everything that went through it discarding the data itself.=20
> or one which
> droped everything to an in-heap list. or even one that split the data
> into two and sent it to multiple sinks... such a framework=20
> would be very
> useful in real world programming.=20

It sounds like what we really need is a way to build your own Handles,
so you can define these kind of filters.  Anyone want to suggest an

> also, Haskell really really needs a standard 'Byte' type rather than
> confusing Char, but I have ranted about this before, I am glad to see
> other people think the same thing... :)..=20

Word8 is the standard byte type.  You can read bytes from a file like
this (untested):

readNBytes :: Handle -> Int -> IO [Word8]
readNBytes h n =3D
	allocaBytes n $ \(p :: Ptr Word8) ->
	  hGetBuf h p n
	  peekArray p n

allocaBytes and peekArray are part of the standard FFI libraries, and
hGetBuf is originally from GHC's IOExts, but I don't see a reason why it
shouldn't be part of the standard IO interface (in fact I've exported it
from System.IO in the core libraries).