Skip to content

Commit cc36165

Browse files
committed
Improve Scaladoc for Either:
- remove text on projections - add for comprehensions
1 parent 3c43a7b commit cc36165

File tree

1 file changed

+48
-24
lines changed

1 file changed

+48
-24
lines changed

src/library/scala/util/Either.scala

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ package scala
1212
package util
1313

1414
/** Represents a value of one of two possible types (a disjoint union.)
15-
* Instances of Either are either an instance of [[scala.util.Left]] or [[scala.util.Right]].
15+
* An instance of Either is either an instance of [[scala.util.Left]] or [[scala.util.Right]].
1616
*
1717
* A common use of Either is as an alternative to [[scala.Option]] for dealing
1818
* with possible missing values. In this usage, [[scala.None]] is replaced
@@ -47,32 +47,48 @@ package util
4747
* Left(23).map(_ * 2) // Left(23)
4848
* }}}
4949
*
50-
* A ''projection'' can be used to selectively operate on a value of type Either,
51-
* depending on whether it is of type Left or Right. For example, to transform an
52-
* Either using a function, in the case where it's a Left, one can first apply
53-
* the `left` projection and invoke `map` on that projected Either. If a `right`
54-
* projection is applied to that Left, the original Left is returned, unmodified.
55-
*
50+
* As Either defines the methods `map` and `flatMap`, it can also be used in for comprehensions:
5651
* {{{
57-
* val l: Either[String, Int] = Left("flower")
58-
* val r: Either[String, Int] = Right(12)
59-
* l.left.map(_.size): Either[Int, Int] // Left(6)
60-
* r.left.map(_.size): Either[Int, Int] // Right(12)
61-
* l.right.map(_.toDouble): Either[String, Double] // Left("flower")
62-
* r.right.map(_.toDouble): Either[String, Double] // Right(12.0)
63-
* }}}
52+
* val right1: Right[Double, Int] = Right(1)
53+
* val right2 = Right(2)
54+
* val right3 = Right(3)
55+
* val left23: Left[Double, Int] = Left(23.0)
56+
* val left42 = Left(42.0)
6457
*
65-
* Like with other types which define a `map` method, the same can be achieved
66-
* using a for-comprehension:
67-
* {{{
68-
* for (s <- l.left) yield s.size // Left(6)
69-
* }}}
58+
* for (
59+
* a <- right1;
60+
* b <- right2;
61+
* c <- right3
62+
* ) yield a + b + c // Right(6)
63+
*
64+
* for (
65+
* a <- right1;
66+
* b <- right2;
67+
* c <- left23
68+
* ) yield a + b + c // Left(23.0)
7069
*
71-
* To support multiple projections as generators in for-comprehensions, the Either
72-
* type also defines a `flatMap` method.
70+
* for (
71+
* a <- right1;
72+
* b <- left23;
73+
* c <- right2
74+
* ) yield a + b + c // Left(23.0)
75+
*
76+
* // It is advisable to provide the type of the “missing” value (especially the right value for `Left`)
77+
* // as otherwise that type might be infered as `Nothing` without context:
78+
* for (
79+
* a <- left23;
80+
* b <- right1;
81+
* c <- left42 // type at this position: Either[Double, Nothing]
82+
* ) yield a + b + c
83+
* // ^
84+
* // error: ambiguous reference to overloaded definition,
85+
* // both method + in class Int of type (x: Char)Int
86+
* // and method + in class Int of type (x: Byte)Int
87+
* // match argument types (Nothing)
88+
* }}}
7389
*
7490
* @author <a href="mailto:[email protected]">Tony Morris</a>, Workingmouse
75-
* @version 1.0, 11/10/2008
91+
* @version 2.0, 2016-07-15
7692
* @since 2.7
7793
*/
7894
sealed abstract class Either[+A, +B] extends Product with Serializable {
@@ -112,8 +128,16 @@ sealed abstract class Either[+A, +B] extends Product with Serializable {
112128
* If this is a `Left`, then return the left value in `Right` or vice versa.
113129
*
114130
* @example {{{
115-
* val l: Either[String, Int] = Left("left")
116-
* val r: Either[Int, String] = l.swap // Result: Right("left")
131+
* val left: Either[String, Int] = Left("left")
132+
* val right: Either[Int, String] = l.swap // Result: Right("left")
133+
* }}}
134+
* @example {{{
135+
* val right = Right(2)
136+
* val left = Left(3)
137+
* for (
138+
* r1 <- right;
139+
* r2 <- left.swap
140+
* ) yield r1 * r2 // Right(6)
117141
* }}}
118142
*/
119143
def swap: Either[B, A] = this match {

0 commit comments

Comments
 (0)