1 % -----------------------------------------------------------------------------
2 % $Id: PrelMarshalError.lhs,v 1.1 2001/01/11 17:25:57 simonmar Exp $
4 % (c) The FFI task force, 2000
7 Marshalling support: Handling of common error conditions
11 module PrelMarshalError (
13 -- throw an exception on specific return values
15 throwIf, -- :: (a -> Bool) -> (a -> String) -> IO a -> IO a
16 throwIf_, -- :: (a -> Bool) -> (a -> String) -> IO a -> IO ()
17 throwIfNeg, -- :: (Ord a, Num a)
18 -- => (a -> String) -> IO a -> IO a
19 throwIfNeg_, -- :: (Ord a, Num a)
20 -- => (a -> String) -> IO a -> IO ()
21 throwIfNull, -- :: String -> IO (Ptr a) -> IO (Ptr a)
23 -- discard return value
34 -- guard an IO operation and throw an exception if the result meets the given
37 -- * the second argument computes an error message from the result of the IO
40 throwIf :: (a -> Bool) -> (a -> String) -> IO a -> IO a
41 throwIf pred msgfct act =
44 (if pred res then ioError . userError . msgfct else return) res
46 -- like `throwIf', but discarding the result
48 throwIf_ :: (a -> Bool) -> (a -> String) -> IO a -> IO ()
49 throwIf_ pred msgfct act = void $ throwIf pred msgfct act
51 -- guards against negative result values
53 throwIfNeg :: (Ord a, Num a) => (a -> String) -> IO a -> IO a
54 throwIfNeg = throwIf (< 0)
56 -- like `throwIfNeg', but discarding the result
58 throwIfNeg_ :: (Ord a, Num a) => (a -> String) -> IO a -> IO ()
59 throwIfNeg_ = throwIf_ (< 0)
61 -- guards against null pointers
63 throwIfNull :: String -> IO (Ptr a) -> IO (Ptr a)
64 throwIfNull = throwIf (== nullPtr) . const
66 -- discard the return value of an IO action
69 void act = act >> return ()