Haskell的EitherT怎么了?

问题描述

当阅读加布里埃尔·冈萨雷斯(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规避了整个EitherTErrorTContTExceptT的混乱。)

解决方法

今天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 ()

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...