fpco / unliftio

The MonadUnliftIO typeclass for unlifting monads to IO
Other
151 stars 51 forks source link

Add `unlift :: MonadUnliftIO m => (IO a -> IO b) -> m a -> m b`? #108

Closed ocharles closed 1 year ago

ocharles commented 1 year ago

What do you think about adding

    unlift :: MonadUnliftIO m => (IO a -> IO b) -> m a -> m b
    unlift m x = do
      runInIO <- askRunInIO
      liftIO $ m $ runInIO x

to Control.Monad.IO.Unlift? This is handy when you have a IO a -> IO b, and avoids a little bit of boilerplate.

Usage example:

runSTControl lock stControl prepare =
  maybe (throwIO Locked) return =<< unlift (withLockTry lock) do

where withLockTry is withLockTry :: Lock -> IO a -> IO (Maybe a) from extra.

snoyberg commented 1 year ago

Seems reasonable to me!