Haskell: lift vs liftIO

后端 未结 3 1275
滥情空心
滥情空心 2021-01-30 05:03

In what situations should liftIO be used? When I\'m using ErrorT String IO, the lift function works to lift IO actions into ErrorT

3条回答
  •  一整个雨季
    2021-01-30 05:35

    liftIO is just a shortcut to the IO Monad, whichever the Monad you are in. Basically, liftIO equals to using a variable number of lifts. At first this might sound redundant but using liftIO has one big advantage: it makes your IO code indpendent of the actual Monad construction so you can reuse the same code no matter the number of layer your final Monad has been built of (this is quite important when writing a monad transformer).

    On the ohter hand, liftIO is not coming for free, as lift does: the Monad transformers you're using must have support for it, e.g. the Monad you're in must be an instance of the MonadIO class, but most Monads nowadays do (and of course, the type-checker will check this for you at compile time: that's the strength of Haskell!).

提交回复
热议问题