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
Eitherinstance directly, or, if you are usingEitherto represent something that may fail you should useErrorTmonad 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With