Skip to content

Commit 63a6e37

Browse files
authored
Merge pull request scala/scala#7011 from jozic/either3
add Left#up and Right#up to upcast to Either
2 parents 9be8c41 + 3e6c71c commit 63a6e37

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

library/src/scala/util/Either.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,15 @@ sealed abstract class Either[+A, +B] extends Product with Serializable {
441441
final case class Left[+A, +B](value: A) extends Either[A, B] {
442442
def isLeft = true
443443
def isRight = false
444+
445+
/**
446+
* Upcasts this `Left[A, B]` to `Either[A, B1]`
447+
* {{{
448+
* Left(1).up[String] // Either[Int, String]
449+
* }}}
450+
*/
451+
def up[B1 >: B]: Either[A, B1] = this
452+
444453
}
445454

446455
/** The right side of the disjoint union, as opposed to the [[scala.util.Left]] side.
@@ -450,6 +459,15 @@ final case class Left[+A, +B](value: A) extends Either[A, B] {
450459
final case class Right[+A, +B](value: B) extends Either[A, B] {
451460
def isLeft = false
452461
def isRight = true
462+
463+
/**
464+
* Upcasts this `Right[A, B]` to `Either[A1, B]`
465+
* {{{
466+
* Right("1").up[Int] // Either[Int, String]
467+
* }}}
468+
*/
469+
def up[A1 >: A]: Either[A1, B] = this
470+
453471
}
454472

455473
object Either {

0 commit comments

Comments
 (0)