Comparing [Fun]Ptrs

Volker Stolz vs at foldr.org
Tue Mar 4 05:35:16 EST 2003


Hi, I'm looking for a way of comparing Ptrs to null *elegantly*.
The FFI distinguishes between 'Ptr a' and 'FunPtr a', so testing
would mean writing ((==) null[Fun]Ptr). This is rather tedious and a predicate
'isNull' might be in order so that it's possible to write

   when (isNull p) $ ...
or
   throwIOErrorIf isNull ...
, especially without having to worry about comparing against the correct
null pointer (FunPtr or Ptr). That's something where a class can help, but
I'm not sure whether such a specialised class is desired...

I'd like to add the following class and instances/functions to Foreign.Ptr
if nobody objects, they'd make code much more readable and concise:

class ToPtr a where
   toPtr :: a -> Ptr b

isNull :: ToPtr a => a -> Bool
isNull = (==) nullPtr . toPtr

The ToPtr class was suggested my Simon M. in favour of an even more specialised
class PtrCmp which just has nullPtr :: PtrCmp a => isNull a -> Bool.
-- 
Volker Stolz * http://www-i2.informatik.rwth-aachen.de/stolz/ * PGP * S/MIME



More information about the FFI mailing list