Proposal: System.Timeout module for base

David Roundy droundy at darcs.net
Sat Jan 27 12:38:27 EST 2007


On Fri, Jan 26, 2007 at 11:48:58PM +0100, Peter Simons wrote:
> +-- |Wrap an 'IO' computation to time out and return @Nothing@ if it hasn't
> +-- succeeded after @n@ microseconds. If the computation finishes before the
> +-- timeout expires, @Just a@ is returned. Timeouts are specified in microseconds
> +-- (@1\/10^6@ seconds). Negative values mean \"wait indefinitely\". When
> +-- specifying long timeouts, be careful not to exceed @maxBound :: Int at .
> +
> +timeout :: Int -> IO a -> IO (Maybe a)

I'd prefer a function of the type

timeout :: Int -> IO a -> IO a

which just throws the exception when there's an error.  I like exception
handling.  Then you could optionally also include a

catchTimeout :: IO a -> IO a -> IO a

to avoid exposing your exception type.

Another question is whether we could have a nicer type for the time
argument.  It'd certainly be nicer if we could use some sort of time type
that has units, but I've not been following the time module at all, so I
don't know if there's an appropriate type.  I'd hate to have to uglify code
with error-prone unit conversions when I might be able to have something
for which the compiler can verify the unit conversions for me.  Otherwise
I'd be tempted to want System.Timeout to export

hour :: Int
second :: Int
day :: Int

which would get silly.
-- 
David Roundy
http://www.darcs.net


More information about the Libraries mailing list