scala Either.RightProjection混淆(为了理解除糖)

时间:2012-05-21 15:49:53

标签: scala for-loop monads either

我可以在scala中使用=进行理解(如6.19 of the SLS部分所述),如下所示:

假设我有一些功能String => Option[Int]

scala> def intOpt(s: String) = try { Some(s.toInt) } catch { case _ => None }
intOpt: (s: String)Option[Int]

然后我可以使用它

scala> for {
   |     str <- Option("1")
   |     i <- intOpt(str)
   |     val j = i + 10    //Note use of = in generator
   |   }
   |   yield j
res18: Option[Int] = Some(11)

我的理解是,这基本上等同于:

scala> Option("1") flatMap { str => intOpt(str) } map { i => i + 10 } map { j => j }
res19: Option[Int] = Some(11)

也就是说,嵌入式生成器是一种将map注入flatMap次调用的方法。到目前为止一切都很好。

Either.RightProjection

我真正想做的事情使用与上一个示例相似的for-comprehension,使用Either monad

但是,如果我们在类似的链中使用它,但这次使用Either.RightProjection monad / functor,它不起作用:

scala> def intEither(s: String): Either[Throwable, Int] = 
  |      try { Right(s.toInt) } catch { case x => Left(x) }
intEither: (s: String)Either[Throwable,Int]

然后使用:

scala> for {
 | str <- Option("1").toRight(new Throwable()).right
 | i <- intEither(str).right //note the "right" projection is used
 | val j = i + 10
 | }
 | yield j
<console>:17: error: value map is not a member of Product with Serializable with Either[java.lang.Throwable,(Int, Int)]
              i <- intEither(str).right
                ^

该问题与右投影期望作为其flatMap方法的参数的函数有关(即它期望R => Either[L, R])。但是修改为不在第二个生成器上调用right,它仍然无法编译。

scala>  for {
 |        str <- Option("1").toRight(new Throwable()).right
 |        i <- intEither(str) // no "right" projection
 |          val j = i + 10
 |      }
 |      yield j
<console>:17: error: value map is not a member of Either[Throwable,Int]
              i <- intEither(str)
                            ^

兆混乱

但现在我倍加困惑。以下工作正常:

scala> for {
 |       x <- Right[Throwable, String]("1").right
 |       y <- Right[Throwable, String](x).right //note the "right" here
 |     } yield y.toInt
res39: Either[Throwable,Int] = Right(1)

但这不是:

scala> Right[Throwable, String]("1").right flatMap { x => Right[Throwable, String](x).right } map { y => y.toInt }
<console>:14: error: type mismatch;
 found   : Either.RightProjection[Throwable,String]
 required: Either[?,?]
              Right[Throwable, String]("1").right flatMap { x => Right[Throwable, String](x).right } map { y => y.toInt }
                                                                                             ^

我认为这些是等价的

  • 发生了什么事?
  • 如何在=中嵌入Either生成器进行理解?

1 个答案:

答案 0 :(得分:9)

您无法将=嵌入for-comprehension这一事实与Jason Zaugg报道的this issue有关;解决方案是右偏Either(或创建与其同构的新数据类型)。

对于你的大混乱,你错误地扩展了糖。贬低

for {
  b <- x(a)
  c <- y(b)
} yield z(c)

x(a) flatMap { b =>
 y(b) map { c =>
  z(c) }} 

而不是

x(a) flatMap { b => y(b)} map { c => z(c) }

因此你应该这样做:

scala> Right[Throwable, String]("1").right flatMap { x => Right[Throwable, String](x).right map { y => y.toInt } }
res49: Either[Throwable,Int] = Right(1)

关于脱毒的更多乐趣(`j = i + 10`问题)

for {
  b <- x(a)
  c <- y(b)
  x1 = f1(b)
  x2 = f2(b, x1)
  ...
  xn = fn(.....)
  d <- z(c, xn)
} yield w(d)

被贬低为

x(a) flatMap { b =>
  y(b) map { c =>
    x1 = ..
    ...
    xn = ..
    (c, x1, .., xn) 
  } flatMap { (_c1, _x1, .., _xn) =>
    z(_c1, _xn) map w }}

因此,在您的情况下,y(b)的结果类型Either没有定义map