当阅读一篇名为 Breaking from a Loop 的旧博客文章(2012 年)时由 Gabriel Gonzalez 撰写,很明显,备受关注的 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
、除了T
一团糟。)
最佳答案
今天 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 ()
关于loops - Haskell 的 EitherT 发生了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63654252/