Scala for-comprehension with tuple decomposition
for { a <- Some(1) b <- Some(2) } yield (a,b) 返回一些((1,2)) for { a <- Right(1).right b <- Left(2).left } yield (a,b) 返回左((1,2)) 现在我想在理解中分解元组. for { (a,b) <- Some((1,2)) (c,d) <- Some((3,4)) } yield (a,b,c,d) 返回一些((1,2,3,4)) for { (a,b) <- Right((1,2)).right (c,d) <- Left((3,4)).left } yield (a,d) 无法编译: error: constructor cannot be instantiated to expected type; found : (T1,T2) required: scala.util.Either[Nothing,(Int,Int)] (a,2)).right error: constructor cannot be instantiated to expected type; found : (T1,T2) required: scala.util.Either[(Int,Int),Nothing] 为什么这个最后一个例子不起作用?有什么不同? 解决方法
这是一个错误:
SI-5589: For-comprehension on Either.RightProjection with Tuple2 extractor in generator fails to compile withFilter()被调用(一些文档引用filter(),但在2.8中有所改变),这与类型推断混淆. withFilter()用于像(a< -b if c)这样的东西,虽然根据6.19它不应该在这种情况下使用. 后一个bug在SI-1336: spec requires type checking of for-comprehension to consider refutability中被捕获,已经开放了七年(2008年). 也许下一代将找到解决方案. 见why does filter have to be defined for pattern matching in a for loop in scala? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |