Search code examples
loopshaskellmonad-transformerseither

What happened to Haskell's EitherT?


When reading an old (2012) blog post titled Breaking from a Loop by Gabriel Gonzalez it became apparent that the spotlighted EitherT has somehow left the ecosystem. The EitherT package states that it is deprecated in favour of either - which actually doesn't concern itself with monad transformers at all.

I noticed that there was an attempt to clean up the error handling mess that seemed to have existed 10 years or so ago. My guess is that EitherT was not needed anymore.

I think the loop-breaking approach presented in the blog post is quite neat. So I'm wondering: What is today's replacement of EitherT. The critique of ErrorT, for this purpose, still stands, I think, and ContT is indeed rather heavy machinery for this little problem.

For reference, here's the idiom discussed in the blog post:

import Control.Monad.Transfomers.EitherT

exit = left 

main = runEitherT $ forever $ do
    str <- lift getLine
    when (str == "exit") $ exit ()

(By the way, there is Control.Break by the same author that circumvents the whole EitherT, ErrorT, ContT, ExceptT mess.)


Solution

  • Today's replacement for EitherT is 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 ()