Safe Haskell | None |
---|---|
Language | Haskell2010 |
This module integrates the lifetimes package with GHC's finalizers; this allows you to have the GC run cleanup actions when a resource is garbage collected, rather than managing its lifetime explicitly.
You should think twice before using this; much of the point of this package is to manage resources whose lifetime is *semantically significant*, so in many cases you will want more control over when the resource is released than this module provides. It would be inappropriate to use this if:
- You need the resource to be cleaned up promptly for semantic reasons (e.g. dropping a network connection).
- The resource is scarce (e.g. file descriptors), so it is not safe to wait for the garbage collector to get around it.
It is sometimes appropriate however, when time of release is mostly an
implementation detail. In particular, this module is fine for use cases
where you would want to use a finalizer anyway, and it can be safer:
The GHC APIs allow you to attach finalizers to arbitrary values, but
doing so is perlious; the compiler and runtime system are free to do
many transformations on the code that uses pure values, so it is easy
to end up with the finalizer being run sooner than you intended. This
module provides a Cell
type for finalizable values which is easier
to reason about.
Documentation
A cell, containing a value with possible finalizers attached. This differs
from Resource
in that getting the underlying value cannot fail, since
cleanup is controlled by the garbage collector.
acquireCell :: Cell a -> Acquire a Source #