val x: Either[Boolean, (Int, Int)] = Right( (5, 10) ) x: E...">

Pattern matching by tuple values ​​within the "for-inclusion" range?

Given:

scala> val x: Either[Boolean, (Int, Int)] = Right( (5, 10) )
x: Either[Boolean,(Int, Int)] = Right((5,10))

I would like to match the pattern by the first and second values ​​of the tuple in x.right, but this did not work:

scala> for { 
     |   (a, b) <- x.right
         | } yield a

<console>:14: error: constructor cannot be instantiated to expected type;
 found   : (T1, T2)
 required: scala.util.Either[Nothing,(Int, Int)]
         (a, b) <- x.right
         ^

I can do something like:

scala> for { a <- x.right } yield a match { case (x, y) => x }
res5: scala.util.Either[Boolean,Int] = Right(5)

But, is there a way to change my first non-compiled code to work?

+4
source share
1 answer

This is a known bug. See SI-7222 .

Now, if you rely on RightProjection, you will need to map the entire tuple and use the _1and accessors _2.

You can also use the right offset Either, for example scalaz.\/, although in this example it will be required Monoid[Boolean].

+6
source

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


All Articles