3
votes

I've read this question. Here is citation of accepted answer:

This instance has been added in base 4.3.x.x, which comes with ghc 7. Meanwhile, you can use the Either instance directly, or, if you are using Either to represent something that may fail you should use ErrorT monad transformer.

I want to use Either for something like this:

> (Left "bad thing happened") >>= \x -> Right (x ++ " ...")
Left "bad thing happened"

So, if one part of the computation fails, its Left is returned.

Actual question is: why should I use ErrorT monad transformer instead of Either monad? I'm a novice in Haskell, and I'm kinda afraid of monad transformters, especially when I'm already writing code inside one.

1

1 Answers

5
votes

I would recommend using Either if it fits your case. An example of where it wouldn't be sufficient is if you want to perform some IO in the middle of your computation, e.g.:

x <- mightReturnLeft
y <- liftIO someIOAction
useXandY x y

In that case, Either isn't sufficient, but ErrorT would work.

Also, I'd recommend using ExceptT instead of ErrorT. ErrorT relies on the Error class which makes it more awkard to use.