Why does this work:
val somePair: Option[(String,String)] = Some(("John", "Doe"))
(for {
pair <- somePair.toRight("Hello unknown!").right
} yield s"Hello ${pair._1} ${pair._2}!").merge
But this doesn't:
val somePair: Option[(String,String)] = Some(("John", "Doe"))
(for {
(name,lastName) <- somePair.toRight("Hello unknown!").right
} yield s"Hello $name $lastName!").merge
Edit:
I should add this is the error message:
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 ^
This is a bug in Scala which is unfortunately open since quite some time.
Take a look at https://issues.scala-lang.org/browse/SI-5589 for reference.
This bug will fixed if you use https://github.com/oleg-py/better-monadic-for
After addind it to dependencies: (name,lastName) <- somePair
works as expected
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.