Bug in touchForeignPtr?

Benjamin Franksen benjamin.franksen at bessy.de
Mon Nov 22 13:25:01 EST 2004

On Monday 22 November 2004 18:55, Sven Panne wrote:
> Abraham Egnor wrote:
> > I've found that when writing an interface to a C library that requires
> > resource management, it's much better to use the withX (see
> > Control.Exception.bracket) style of function than to use finalizers -
> > programs are much easier to reason about and debug.
> ... and have a much more deterministic behaviour regarding resource
> consumption. This is a very important point which is often forgotten.

Ok, the 'withX' pattern solves both of the problems I mentioned and in 
addition is more deterministic (and, using bracket, also exception safe). 
Maybe I should use it instead of finalizers. However, I wonder if it is 
general enough to be the only method exported from a library. I doubt it, 
otherwise there would be no 'openFile' and 'hClose' in System.IO but only a 

Do you recommend supplying 'newX' and 'freeX' in addition to 'withX'?

Or should I rather use 'newX' plus finalizers for those (rare?) cases where 
'withX' is not general enough?

Top level things with identity are evil.	-- Lennart Augustsson

More information about the Glasgow-haskell-users mailing list