1 % -----------------------------------------------------------------------------
2 % $Id: PrelMarshalError.lhs,v 1.2 2001/05/18 16:54:05 simonmar Exp $
4 % (c) The FFI task force, 2000
7 Marshalling support: Handling of common error conditions
10 {-# OPTIONS -fno-implicit-prelude #-}
12 module PrelMarshalError (
14 -- throw an exception on specific return values
16 throwIf, -- :: (a -> Bool) -> (a -> String) -> IO a -> IO a
17 throwIf_, -- :: (a -> Bool) -> (a -> String) -> IO a -> IO ()
18 throwIfNeg, -- :: (Ord a, Num a)
19 -- => (a -> String) -> IO a -> IO a
20 throwIfNeg_, -- :: (Ord a, Num a)
21 -- => (a -> String) -> IO a -> IO ()
22 throwIfNull, -- :: String -> IO (Ptr a) -> IO (Ptr a)
24 -- discard return value
37 -- guard an IO operation and throw an exception if the result meets the given
40 -- * the second argument computes an error message from the result of the IO
43 throwIf :: (a -> Bool) -> (a -> String) -> IO a -> IO a
44 throwIf pred msgfct act =
47 (if pred res then ioError . userError . msgfct else return) res
49 -- like `throwIf', but discarding the result
51 throwIf_ :: (a -> Bool) -> (a -> String) -> IO a -> IO ()
52 throwIf_ pred msgfct act = void $ throwIf pred msgfct act
54 -- guards against negative result values
56 throwIfNeg :: (Ord a, Num a) => (a -> String) -> IO a -> IO a
57 throwIfNeg = throwIf (< 0)
59 -- like `throwIfNeg', but discarding the result
61 throwIfNeg_ :: (Ord a, Num a) => (a -> String) -> IO a -> IO ()
62 throwIfNeg_ = throwIf_ (< 0)
64 -- guards against null pointers
66 throwIfNull :: String -> IO (Ptr a) -> IO (Ptr a)
67 throwIfNull = throwIf (== nullPtr) . const
69 -- discard the return value of an IO action
72 void act = act >> return ()