[Haskell-beginners] Tips for a FFI-wrapped C library?

Stephen Tetley stephen.tetley at gmail.com
Tue Dec 1 04:46:10 EST 2009

Hi Patrick

One thing you can do is use `bracket` to manage the lifetime of
objects created on the C side:

withNewThing :: Init_param1 -> Init_param2 -> (Thing -> IO a) -> IO a
withNewThing param1 param2 funAction =
  bracket (newThing param1 param2) destroyThing
          (\thing -> do { check_null <- isNullThing thing
                        ; if check_null
                            then do  -- SOME FAILURE --
                            else funAction face })

-- newThing destroyThing isNullThing are defined somewhere.

This might be appropriate for the Context, Language etc. objects in
your library.

For the documentation on bracket see Control.Exception.Base.

Best wishes


More information about the Beginners mailing list