scala 获取任何一个的价值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19503647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting Value of Either
提问by Kevin Meredith
Besides using match, is there an Option-like way to getOrElsethe actual content of the Rightor Leftvalue?
除了使用match,是否有类似 Option 的方式来getOrElse获取RightorLeft值的实际内容?
scala> val x: Either[String,Int] = Right(5)
scala> val a: String = x match {
case Right(x) => x.toString
case Left(x) => "left"
}
a: String = 5
采纳答案by Nicolas Rinaudo
I don't particularly like Eitherand as a result I'm not terribly familiar with it, but I believe you're looking for projections: either.left.getOrElseor either.right.getOrElse.
我不是特别喜欢Either,因此我对它并不十分熟悉,但我相信您正在寻找预测:either.left.getOrElse或either.right.getOrElse.
Note that projections can be used in for-comprehensions as well. This is an example straight from the documentation:
请注意,预测也可用于理解。这是直接来自文档的示例:
def interactWithDB(x: Query): Either[Exception, Result] =
try {
Right(getResultFromDatabase(x))
} catch {
case ex => Left(ex)
}
// this will only be executed if interactWithDB returns a Right
val report =
for (r <- interactWithDB(someQuery).right) yield generateReport(r)
if (report.isRight)
send(report)
else
log("report not generated, reason was " + report.left.get)
回答by Kristian Domagala
Nicolas Rinaudo's answer regarding calling getOrElseon either the leftor rightprojection is probably the closest to Option.getOrElse.
萨科瑙多的关于呼吁答案getOrElse在任一left或right投影可能是最接近Option.getOrElse。
Alternatively, you can foldthe either:
或者,您可以fold:
scala> val x: Either[String,Int] = Right(5)
x: Either[String,Int] = Right(5)
scala> val a: String = x.fold(l => "left", r => r.toString)
a: String = 5
As lis not used in the above fold, you could also write x.fold(_ => "left", r => r.toString)
由于l没有在上面的折叠中使用,你也可以写x.fold(_ => "left", r => r.toString)
Edit:Actually, you can literally have Option.getOrElseby calling toOptionon the leftor rightprojection of the either, eg,
编辑:实际上,您可以Option.getOrElse通过调用toOption两者之一的left或right投影来真正拥有,例如,
scala> val o: Option[Int] = x.right.toOption
o: Option[Int] = Some(5)
scala> val a: String = o.map(_.toString).getOrElse("left")
a: String = 5
回答by Mario Galic
Given type Aon both sides, that is, Either[A, A], we can use Either.merge
给定A两边的类型,即Either[A, A],我们可以使用Either.merge
...to extract values from Either instances regardless of whether they are Left or Right.
...从Either 实例中提取值,而不管它们是Left 还是Right。
Note if left and right types differ then result becomes Any:
请注意,如果左右类型不同,则结果变为Any:
val e: Either[Int, String] = Right("hello")
e.merge // hello: Any
回答by lex82
In Scala 2.12 there is a getOrElsemethod for getting the "right" value but you cannot use it for the "left" value directly. However, you can do it like this: e.swap.getOrElse(42).
在 Scala 2.12 中有一种getOrElse获取“右”值的方法,但不能直接将它用于“左”值。但是,你可以做这样的:e.swap.getOrElse(42)。

