ResourceT: Safe allocation and freeing of resources

November 23, 2020 ยท View on GitHub

ResourceT is a monad transformer that creates a region of code where you can safely allocate resources. These resources can then be freed manually on demand, or automatically when execution reaches the end of the region or when an unrecovered error occurs within the region.

Is this like Aff.bracket?

Yes, somewhat. But you can consider this the transformer version of bracket giving access to easier sequential composition. To this point bracket can be implemented using ResourceT, while the reverse is much more involved.

import Control.Monad.Resource (class MonadResource, acquire, release)

bracket :: forall m a. MonadResource m => Aff a -> (a -> Aff Unit) -> (a -> m b) -> m b
bracket allocate free run = do
  key /\ resource <- acquire allocate free
  a <- run resource
  release key
  pure a