Either is a type used in functional languages such as Haskell and Scala to represent a value that is one of two parametrized types. In Scala, the Either type is often used as an alternative to scala.Option where Left represents failure (by convention) and Right is akin to Some.
Questions tagged [either]
341 questions
23
votes
2 answers
Try[Result], IO[Result], Either[Error,Result], which should I use in the end
I'd like to know what should be the signature of my methods so that I handle different kind of failures elegantly.
This question is somehow the summary of many questions I already had about error handling in Scala. You can find some questions…

Sebastien Lorber
- 89,644
- 67
- 288
- 419
22
votes
3 answers
how do I process returned Either
if a scala function is
def A(): Either[Exception, ArrayBuffer[Int]] = {
...
}
what should be the right way to process the returned result?
val a = A()
and ?

user398384
- 1,124
- 3
- 14
- 21
21
votes
1 answer
Why `scala.util.Try` is not mentioned in chapter "Handling errors without exceptions" of book "functional programming in Scala"?
In the chapter "Handling errors without exceptions" of book "functional programming in Scala", the author gives:
The problem of throwing exceptions from the body of a function
Use Option if we don't care about the actual exception
Use Either if we…

Freewind
- 193,756
- 157
- 432
- 708
20
votes
1 answer
Layering State with Either in scalaz
In Integrating State with Either (slide 88), given the pattern of State layered under Either, is there a recommended approach for adding another type of state, e.g., logging via something like Writer? It seems the new state has to live between the…

Sim
- 13,147
- 9
- 66
- 95
19
votes
2 answers
Validation versus disjunction
Suppose I want to write a method with the following signature:
def parse(input: List[(String, String)]):
ValidationNel[Throwable, List[(Int, Int)]]
For each pair of strings in the input, it needs to verify that both members can be parsed as…

Travis Brown
- 138,631
- 12
- 375
- 680
17
votes
2 answers
Why is there no alternative instance for Either but a semigroup that behaves similarily to alternative?
I am a Haskell newbie and I wonder why there is no alternative instance for Either but a semigroup, which behaves as I would expect it from alternative:
instance Semigroup (Either a b) where
Left _ <> b = b
a <> _ = a
This instance discards or…
user6445533
16
votes
4 answers
Is there no standard (Either a) monad instance?
I was under the impression that there was an instance for Either a somewhere, but I can't seem to find it. I have tried importing Control.Monad, Control.Monad.Instances and Data.Either as shown
module Main where
import Control.Monad
import…

Boris
- 5,094
- 4
- 45
- 71
13
votes
2 answers
Why do `Left` and `Right` have two type parameters?
I understand it would be difficult to change now without breaking existing code, but I'm wondering why it was done that way in the first place.
Why not just:
sealed trait Either[+A, +B]
case class Left[A](x: A) extends Either[A, Nothing]
case class…

Seth Tisue
- 29,985
- 11
- 82
- 149
13
votes
4 answers
How to accumulate errors in Either?
Suppose I have few case classes and functions to test them:
case class PersonName(...)
case class Address(...)
case class Phone(...)
def testPersonName(pn: PersonName): Either[String, PersonName] = ...
def testAddress(a: Address): Either[String,…

Michael
- 41,026
- 70
- 193
- 341
12
votes
1 answer
Trying to implement Data.Either
To help me learn Applicative Functors and Functors I thought it would be good fun to see how Either is implemented with the typeclasses Functor and Applicative. Obviously I could just go ahead and read the code but I find it more useful to try and…

djhworld
- 6,726
- 4
- 30
- 44
12
votes
1 answer
scala Either.RightProjection confusion (for comprehension de-sugaring)
I can use an = in a scala for-comprehension (as specified in section 6.19 of the SLS) as follows:
Option
Suppose I have some function String => Option[Int]:
scala> def intOpt(s: String) = try { Some(s.toInt) } catch { case _ => None }
intOpt: (s:…

oxbow_lakes
- 133,303
- 56
- 317
- 449
10
votes
1 answer
How to make Either into a functor in second type
I have been reading "Learn You A Haskell For Great Good!" and right now I am on "The Functor Typeclass" section.
Here they make Either into a functor by fixing the first type as follows:
instance Functor (Either a) where
fmap f (Right x) = Right…

kishlaya
- 453
- 2
- 14
10
votes
3 answers
lift Either to ExceptT automatically
Let's say I have this (arguably mislead) piece of code laying around:
import System.Environment (getArgs)
import Control.Monad.Except
parseArgs :: ExceptT String IO User
parseArgs =
do
args <- lift getArgs
case safeHead args of
Just…

romeovs
- 5,785
- 9
- 43
- 74
10
votes
1 answer
How do I program monadicaly in Java8 while getting comparable performance?
Is monadic programming in Java 8 slower? Below is my test (a right-biased Either is used that creates new instances for each computation). The imperative version is 1000 times faster. How do I program monadicaly in Java8 while getting comparable…

jordan3
- 877
- 5
- 13
10
votes
2 answers
Idiomatic way to write firstRightOrLefts in Haskell?
I have the following method:
firstRightOrLefts :: [Either b a] -> Either [b] a
firstRightOrLefts eithers =
case partitionEithers eithers of
(_, (x : _)) -> Right x
(xs, _) -> Left xs
What bothers me is the ugly pattern…

Robert Massaioli
- 13,379
- 7
- 57
- 73