为什么会这样:
val somePair: Option[(String,String)] = Some(("John", "Doe"))
(for {
pair <- somePair.toRight("Hello unknown!").right
} yield s"Hello ${pair._1} ${pair._2}!").merge
但这并不:
val somePair: Option[(String,String)] = Some(("John", "Doe"))
(for {
(name,lastName) <- somePair.toRight("Hello unknown!").right
} yield s"Hello $name $lastName!").merge
- 编辑:**
我应该补充这是错误消息:Error:(43, 4) constructor cannot be instantiated to expected type; found : (T1, T2) required: scala.util.Either[Nothing,(String, String)] (name,lastName) <- somePair.toRight("Hello unknown!").right ^
2条答案
按热度按时间tp5buhyn1#
这是Scala中的一个bug,很不幸,它已经公开了很长一段时间。
请参考https://issues.scala-lang.org/browse/SI-5589。
xxslljrj2#
如果使用https://github.com/oleg-py/better-monadic-for,此错误将得到修复
将其添加到依赖项后:
(name,lastName) <- somePair
按预期工作