Why can't I extract a tuple from any projection inward for understanding using pattern matching?

Why does it 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 is not so:

val somePair: Option[(String,String)] = Some(("John", "Doe"))
(for {
  (name,lastName) <- somePair.toRight("Hello unknown!").right
} yield s"Hello $name $lastName!").merge

Edit:
I have to add this 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 ^

+4
source share
1 answer

This is a mistake in Scala, which, unfortunately, has been open since some time.

Take a look at https://issues.scala-lang.org/browse/SI-5589 for help.

+3
source

Source: https://habr.com/ru/post/1671191/


All Articles