Null Pointer Pattern

Henning Thielemann lemming at henning-thielemann.de
Tue Aug 18 06:54:52 UTC 2020


On Mon, 17 Aug 2020, Carter Schonwald wrote:

> There’s an interesting idea or two in here.  Like, should we support 
> having non-nullable pointers?

That was my first idea, too.

newtype NonNullPtr a = NonNullPtr (Ptr a)

maybeNonNull :: Ptr a -> Maybe (NonNullPtr a)

Using NonNullPtr in FFI declarations would make the interfaces safer.

E.g.
    dst <- mallocArray n
    for (maybeNonNull dst) $ \dstNonNull ->
       copyArray dstNonNull srcNonNull n

That seems better than testing for nullPtr and then forgeting this 
information in the type, again.


More information about the Libraries mailing list