7

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 ^

shayan
  • 1,211
  • 9
  • 12

2 Answers2

4

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.

Sebastian
  • 16,813
  • 4
  • 49
  • 56
-1

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

f4x
  • 126
  • 1
  • 4
  • 14
  • f4x, please don't just post some tool or library as an answer. At least demonstrate [how it solves the problem](//meta.stackoverflow.com/a/251605) in the answer itself. – jmoerdyk Feb 01 '23 at 21:45
  • I have no idea what I shoud demonstrate - just add the library to dependencies and `(name,lastName) <- somePair` works as expected – f4x Feb 01 '23 at 21:52
  • What is the reason for the decrease rating of answer? I searched the recipe, I found it and shared. It is crazy for me that a simple constataion of problem: yes, there is a bug in the compiler has positive rating and the solution of the problem is negative. If I will be searching solution of the problem it will be confusing me. – f4x Feb 01 '23 at 22:08