问题描述
当阅读加布里埃尔·冈萨雷斯(Gabriel Gonzalez)题为Breaking from a Loop的旧博客文章(2012年)时,很明显,受到关注的EitherT
以某种方式离开了生态系统。 EitherT package声明不赞成使用either-实际上,它与monad转换器完全无关。
我注意到有人试图清理似乎已经存在10年左右的错误处理混乱。我的猜测是EitherT
不再需要。
我认为博客文章中介绍的打破循环的方法非常简洁。所以我想知道:EitherT
的今天替代品是什么。我认为对ErrorT
的批评仍然存在,而ContT
确实是解决这个小问题的笨拙机器。
import Control.Monad.Transfomers.EitherT
exit = left
main = runEitherT $ forever $ do
str <- lift getLine
when (str == "exit") $ exit ()
(顺便说一下,同一位作者的Control.Break规避了整个EitherT
,ErrorT
,ContT
,ExceptT
的混乱。)
解决方法
今天EitherT
的替换项是ExceptT
:
import Control.Monad.Except
-- use `throwError` in place of `left`
exit = throwError
-- use `runExceptT` in place of `runEitherT`
main = runExceptT $ forever $ do
str <- lift getLine
when (str == "exit") $ exit ()