[Haskell-cafe] Why Either = Left | Right instead of something like Result = Success | Failure

Ivan Lazar Miljenovic ivan.miljenovic at gmail.com
Fri May 28 06:45:17 EDT 2010


"Ionut G. Stan" <ionut.g.stan at gmail.com> writes:

> Thank you all for the answers. It seems that a recurring reason is
> that being defined as a general type, Either can be used not only to
> handle errors, but choice in general. This, however, seems to make
> Either to overloaded in my opinion. If I decide that I want to use
> Either as a way to signal error, but a third party accepts Either for
> something different, then things will probably not work.

You could then categorise a lot of types as being overloaded; for
example Maybe is used to indicate success/failure, a list with no more
than one value, etc.

> I have little experience with Haskell, but I haven't seen Either used
> in contexts other than error/success. If you could point me to some
> piece of code that uses it in a different way it would be great.

I've used it with partitionEithers to map a function that categorises a
list of values into two types (typically program arguments) and then
split them up.

> I'll probably use something different as an internal representation of
> failure in my programs, and only keep Either for the public API. Just
> my thoughts for the moment...

Unless you have a _very_ good reason to do this, I wouldn't for the sole
reason that its yet another thing someone has to comprehend if they want
to read your code.  Whilst the Either type isn't officially used for
errors, that is how it is usually treated in Haskell with the consensus
that Left = failure and Right = success (note that due to how its
defined it also has to be this way for Either's Monad instance to
work).

-- 
Ivan Lazar Miljenovic
Ivan.Miljenovic at gmail.com
IvanMiljenovic.wordpress.com


More information about the Haskell-Cafe mailing list