scala Either.RightProjection 混淆(用于理解去糖)
题
我可以使用 =
在 scala 的理解中(如章节中指定的) 6.19 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
来电。到目前为止,一切都很好。
任一右投影
我真正想做的: 使用与前面的示例类似的 for-compression,使用 Either
单子.
但是,如果我们在类似的链中使用它,但这次使用 Either.RightProjection
monad/函子,它不起作用:
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 }
^
我认为这些是等价的
- 到底是怎么回事?
- 我怎样才能嵌入
=
a 中的生成器用于理解 anEither
?
解决方案
事实上,你不能嵌入 =
在 for 理解中与 这个问题 杰森·扎格报道;解决方案是右偏 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
定义的。
不隶属于 StackOverflow