@@ -12,7 +12,7 @@ package scala
12
12
package util
13
13
14
14
/** 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 ]].
16
16
*
17
17
* A common use of Either is as an alternative to [[scala.Option ]] for dealing
18
18
* with possible missing values. In this usage, [[scala.None ]] is replaced
@@ -47,32 +47,48 @@ package util
47
47
* Left(23).map(_ * 2) // Left(23)
48
48
* }}}
49
49
*
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:
56
51
* {{{
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)
64
57
*
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)
70
69
*
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
+ * }}}
73
89
*
74
90
* @author <a href="mailto:[email protected] ">Tony Morris</a>, Workingmouse
75
- * @version 1 .0, 11/10/2008
91
+ * @version 2 .0, 2016-07-15
76
92
* @since 2.7
77
93
*/
78
94
sealed abstract class Either [+ A , + B ] extends Product with Serializable {
@@ -112,8 +128,16 @@ sealed abstract class Either[+A, +B] extends Product with Serializable {
112
128
* If this is a `Left`, then return the left value in `Right` or vice versa.
113
129
*
114
130
* @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)
117
141
* }}}
118
142
*/
119
143
def swap : Either [B , A ] = this match {
0 commit comments