@@ -8,11 +8,11 @@ title: SIP-20 Fixing Either
8
8
9
9
This proposal is the sole initiative of the author, based on his
10
10
attempts, [ first] [ enhance ] to understand why ` scala.Either ` is not
11
- more widely used, and to enhance it, [ and then] [ fix ] to try to fix
12
- it. It is also being made in the light of two mailing-list debates
13
- which have taken place - one about [ right-biasing Either ] [ debate1 ] ,
14
- which was inconclusive, and a subsequent one about [ fixing
15
- Either] [ debate2 ] .
11
+ more widely used, and to enhance it, and then ( [ here ] [ fix ] and
12
+ [ here ] [ vs ] ) to try to fix it. It is also being made in the light of
13
+ two mailing-list debates which have taken place - one (initiated by
14
+ Jason Zaugg) about [ right-biasing Either ] [ debate1 ] , which was
15
+ inconclusive, and a subsequent one about [ fixing Either] [ debate2 ] .
16
16
17
17
## Motivation ##
18
18
@@ -21,11 +21,11 @@ eschewed in favour of alternatives:
21
21
22
22
1 . ` for ` comprehensions involving ` Either ` behave oddly.
23
23
24
- 2 . The alternatives are simpler to use, by virtue of their being 'biased'
25
- towards one of their possible result types. In the case of
24
+ 2 . The alternatives are simpler to use, by virtue of their being
25
+ 'biased' towards one of their possible result types. In the case of
26
26
` Either ` , it is necessary to specify which of the two types (` Left `
27
- or ` Right ` ) should have its value passed to ` foreach ` , ` map ` or
28
- ` flatMap ` .
27
+ or ` Right ` ) should have its value passed to the function passed to
28
+ ` foreach ` , ` map ` or ` flatMap ` .
29
29
30
30
3 . The alternative may offer 'added value', such as ` scalaz.Validation ` 's
31
31
ability to accumulate failures.
@@ -45,7 +45,7 @@ definitions are not supported (as first reported [here][report]),
45
45
c = b + 1
46
46
} ... // do something with c
47
47
48
- and secondly (as reported [ here] [ fix ] ), ` if ` cannot be used together
48
+ and secondly (as mentioned [ here] [ fix ] ), ` if ` cannot be used together
49
49
with multiple generators and ` yield ` :
50
50
51
51
for {
@@ -59,12 +59,12 @@ with multiple generators and `yield`:
59
59
} yield c
60
60
61
61
The lack of support for definitions was traced to the fact that the
62
- ` map ` method of LeftProjection and RightProjection returns an
62
+ ` map ` method of ` LeftProjection ` and ` RightProjection ` returns an
63
63
` Either ` , which does not have a ` foreach ` , or ` map ` method itself.
64
64
65
65
The proposed solution is the one proposed [ here] [ fix ] , whereby the
66
- ` map ` method of ` LeftProjection ` returns another ` LeftProjection ` , and
67
- that of ` RightProjection ` returns another ` RightProjection ` .
66
+ ` map ` method of ` LeftProjection ` ( ` RightProjection ` ) returns another
67
+ ` LeftProjection ` ( ` RightProjection ` ) .
68
68
69
69
Also, and as a conseqence, the respective ` flatMap ` methods must be
70
70
changed by substituting the respective projection in place of
@@ -106,7 +106,7 @@ becomes
106
106
case Right(b) => LeftProj(Right(b))
107
107
}
108
108
109
- Note that ` .e ` must be appended to the value the for- comprehension then
109
+ Note that ` .e ` must be appended to the value the ` for ` comprehension then
110
110
yields, in order to obtain the corresponding ` Either ` value.
111
111
112
112
Regarding the second example of odd behaviour, involving ` if ` , this was
@@ -126,10 +126,10 @@ pattern-matching in `for` comprehensions involving (projections of)
126
126
127
127
Therefore, a third solution has been investigated, whereby ` LeftProj `
128
128
(` RightProj ` ) has a ` withFilter ` method that returns a ` LeftProj `
129
- containing a ` Right ` (` RightProj ` containing a ` Left ` ) if the
130
- predicate is ` false ` , and where the contents of that ` Right ` (` Left ` )
131
- is obtained using an implicit conversion passed to the ` withFilter `
132
- method in a second parameter list:
129
+ (` RightProj ` ) containing a ` Right ` ( ` Left ` ) if the predicate is
130
+ ` false ` , and where the contents of that ` Right ` (` Left ` ) is obtained
131
+ using an implicit conversion passed to the ` withFilter ` method in a
132
+ second parameter list:
133
133
134
134
def withFilter[BB >: B](p: A => Boolean)
135
135
(implicit aToB: Right.Convert => BB): LeftProj[A, BB] = {
@@ -147,9 +147,9 @@ when `if` or pattern-matching features in a `for` comprehension:
147
147
implicit def f(convert: Right.Convert) = convert.any.toString
148
148
149
149
Note that ` Convert ` is a simple case class which serves to ensure that
150
- the implicit conversion is properly targetted .
150
+ the implicit conversion is properly targeted .
151
151
152
- This third solution has been demonstrated to work well, and is the one
152
+ This third solution has been [ demonstrated] [ project ] to work well, and is the one
153
153
proposed here.
154
154
155
155
## Part 2: Simplifying use in for-comprehensions by * adding* right-biased capability ##
@@ -183,11 +183,19 @@ Finally, note that Part 2 does not render Part 1 redundant; although
183
183
appropriate in ` for ` comprehensions involving the ` RightProjection ` ,
184
184
but not the ` LeftProjection ` .
185
185
186
+ ## Trial version ##
187
+
188
+ A trial verson of ` Either ` incorporating the proposed enhancements,
189
+ and complete with test suites, is maintained [ here] [ project ] .
190
+
186
191
[ enhance ] : http://robsscala.blogspot.co.uk/2012/04/validation-without-scalaz.html
187
192
[ fix] :
188
193
http://robsscala.blogspot.co.uk/2012/05/fixing-scalaeither-leftrightmap-returns.html
194
+ [ vs] :
195
+ http://robsscala.blogspot.co.uk/2012/06/fixing-scalaeither-unbiased-vs-biased.html
189
196
[ debate1] :
190
197
https://groups.google.com/group/scala-debate/browse_thread/thread/2bac2fe8aa6124ad?hl=en
191
198
[ debate2] :
192
199
https://groups.google.com/forum/?fromgroups#!topic/scala-debate/XlN-oqbslS0
193
200
[ report ] : https://issues.scala-lang.org/browse/SI-5793
201
+ [ project ] : https://github.com/robcd/scala-either-proj-map-returns-proj/tree/add_right-bias_2-10_withFilter
0 commit comments