Type of newForeignPtr & addForeignPtrFinalizer

Adrian Hey ahey@iee.org
Tue, 23 Jul 2002 14:57:43 +0100

On Monday 22 July 2002 12:33 pm, Simon Marlow wrote:
> > The second seems to require this bit of weirdness..
> >  myNewForeignPtr :: Ptr a -> (ForeignPtr a -> IO ()) -> IO
> > (ForeignPtr a)
> >  myNewForeignPtr p fin = do
> >    newfp  <- newForeignPtr p (return ())
> >    addForeignPtrFinalizer newfp (fin newfp)
> >    return newfp
> You can do this more easily using fixIO:
>    myNewForeignPtr p fin = do
> 	fixIO (\fp -> newForeignPtr p (fin fp))

Thanks, neat (I think:-). I wonder if I might indulge myself with
another stupid question related to this, that is, why make the
distinction between Ptr and ForeignPtr at all?

By definition a ForeignPtr has a non-zero number of finalisers
and Ptr has no finalisers. Couldn't you just allow ForeignPtr's
to have no finalisers and dispense with Ptr alltogether? Then
you could just add finalisers as required rather than converting
between types. It seems that when making a foreign binding you have
to make what seems (to me) an arbitrary choice between Ptr and
ForeignPtr arguments. I don't really understand the reason for
this extra complexity.

Adrian Hey