/
Or.scala
1362 lines (1316 loc) · 66.7 KB
/
Or.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2001-2013 Artima, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.scalactic
import scala.util.Try
import scala.util.Success
import scala.util.Failure
import scala.util.control.NonFatal
import scala.collection.GenTraversableOnce
import scala.collection.generic.CanBuildFrom
import scala.collection.mutable.Builder
/**
* Represents a value that is one of two possible types, with one type being “good” and
* the other “bad.”
*
* <p>
* An <code>Or</code> will either be a “good” value wrapped in an instance of
* <a href="Good.html"><code>Good</code></a> or a “bad” value wrapped in an instance
* of <a href="Bad.html"><code>Bad</code></a>.
* </p>
*
* <h2>The motivation for <code>Or</code></h2>
*
* <p>
* <code>Or</code> differs from Scala's <code>Either</code> type in that
* <code>Either</code> treats both its <code>Left</code> and <code>Right</code> alternatives in an identical manner, whereas
* <code>Or</code> treats its two alternatives differently: it favors
* <code>Good</code> over <code>Bad</code>.
* Because of this, it is more convenient to work with <code>Or</code>s
* when you prefer one alternative over the other; for example, if one alternative represents a valid result
* and another represents an error.
* </p>
*
* <p>
* To illustrate, imagine you want to create instances this <code>Person</code> class from user input strings:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">case</span> <span class="stReserved">class</span> <span class="stType">Person</span>(name: <span class="stType">String</span>, age: <span class="stType">Int</span>)
* </pre>
*
* <p>
* You might write a method that parses the name from user input string and returns an
* <code>Option[String]</code>: <code>None</code> if the string is empty or blank, else the
* trimmed string wrapped in a <code>Some</code>:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">def</span> parseName(input: <span class="stType">String</span>): <span class="stType">Option[String]</span> = {
* <span class="stReserved">val</span> trimmed = input.trim
* <span class="stReserved">if</span> (!trimmed.isEmpty) <span class="stType">Some</span>(trimmed) <span class="stReserved">else</span> <span class="stType">None</span>
* }
* </pre>
*
* <p>
* You might also write a method that parses the age from user input string and returns an
* <code>Option[Int]</code>: <code>None</code> if either the string is not a valid integer or
* it is a negative integer, else the string converted to an integer wrapped in a <code>Some</code>:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">def</span> parseAge(input: <span class="stType">String</span>): <span class="stType">Option[Int]</span> = {
* <span class="stReserved">try</span> {
* <span class="stReserved">val</span> age = input.trim.toInt
* <span class="stReserved">if</span> (age >= <span class="stLiteral">0</span>) <span class="stType">Some</span>(age) <span class="stReserved">else</span> <span class="stType">None</span>
* }
* <span class="stReserved">catch</span> {
* <span class="stReserved">case</span> _: <span class="stType">NumberFormatException</span> => <span class="stType">None</span>
* }
* }
* </pre>
*
* <p>
* With these building blocks you could write a method that parses name and age input
* strings and returns either a <code>Person</code>, wrapped in a <code>Some</code>, or
* <code>None</code> if either the name or age, or both, was invalid:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">def</span> parsePerson(inputName: <span class="stType">String</span>, inputAge: <span class="stType">String</span>): <span class="stType">Option[Person]</span> =
* <span class="stReserved">for</span> {
* name <- parseName(inputName)
* age <- parseAge(inputAge)
* } <span class="stReserved">yield</span> <span class="stType">Person</span>(name, age)
* </pre>
*
* <p>
* Here are some examples of invoking <code>parsePerson</code>:
* </p>
*
* <pre class="stHighlighted">
* parsePerson(<span class="stQuotedString">"Bridget Jones"</span>, <span class="stQuotedString">"29"</span>)
* <span class="stLineComment">// Result: Some(Person(Bridget Jones,29))</span>
* <br/>parsePerson(<span class="stQuotedString">"Bridget Jones"</span>, <span class="stQuotedString">""</span>)
* <span class="stLineComment">// Result: None</span>
* <br/>parsePerson(<span class="stQuotedString">"Bridget Jones"</span>, <span class="stQuotedString">"-29"</span>)
* <span class="stLineComment">// Result: None</span>
* <br/>parsePerson(<span class="stQuotedString">""</span>, <span class="stQuotedString">""</span>)
* <span class="stLineComment">// Result: None</span>
* </pre>
*
* <p>
* Now imagine you want to give an error message back if the user's input is invalid.
* You might rewrite the parsing methods to return an <code>Either</code> instead. In this
* case, the desired result is a valid name or age, which by convention should be placed
* on the right of the <code>Either</code>. The left will be a <code>String</code> error
* message. Here's the new <code>parseName</code> function, which returns an <code>Either[String, String]</code>:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">def</span> parseName(input: <span class="stType">String</span>): <span class="stType">Either[String, String]</span> = {
* <span class="stReserved">val</span> trimmed = input.trim
* <span class="stReserved">if</span> (!trimmed.isEmpty) <span class="stType">Right</span>(trimmed) <span class="stReserved">else</span> <span class="stType">Left</span>(s<span class="stQuotedString">""""${input}" is not a valid name"""</span>)
* }
* </pre>
*
* <p>
* And here's the new <code>parseAge</code> function, which returns an <code>Either[String, Int]</code>:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">def</span> parseAge(input: <span class="stType">String</span>): <span class="stType">Either[String, Int]</span> = {
* <span class="stReserved">try</span> {
* <span class="stReserved">val</span> age = input.trim.toInt
* <span class="stReserved">if</span> (age >= <span class="stLiteral">0</span>) <span class="stType">Right</span>(age) <span class="stReserved">else</span> <span class="stType">Left</span>(s<span class="stQuotedString">""""${age}" is not a valid age"""</span>)
* }
* <span class="stReserved">catch</span> {
* <span class="stReserved">case</span> _: <span class="stType">NumberFormatException</span> => <span class="stType">Left</span>(s<span class="stQuotedString">""""${input}" is not a valid integer"""</span>)
* }
* }
* </pre>
*
* <p>
* The new <code>parsePerson</code> method will return an <code>Either[String, Person]</code>:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">def</span> parsePerson(inputName: <span class="stType">String</span>, inputAge: <span class="stType">String</span>): <span class="stType">Either[String, Person]</span> =
* <span class="stReserved">for</span> {
* name <- parseName(inputName).right
* age <- parseAge(inputAge).right
* } <span class="stReserved">yield</span> <span class="stType">Person</span>(name, age)
* </pre>
*
* <p>
* Note that <code>Either</code> requires you to add <code>.right</code>
* at the end of each generator in the <code>for</code> expression. Although the convention is to place the
* valid result on the right, you must explicitly (and repetitively) indicate that you've done so by transforming
* the <code>Either</code> to a <code>RightProjection</code> by invoking <code>.right</code> at each step.
* Given this implementation, the <code>parsePerson</code> method will now short-circuit at the first sign
* of trouble (as it did when we used an <code>Option</code>), but you now get the first error message returned
* in a <code>Left</code>. Here are some examples:
* </p>
*
* <pre class="stHighlighted">
* parsePerson(<span class="stQuotedString">"Bridget Jones"</span>, <span class="stQuotedString">"29"</span>)
* <span class="stLineComment">// Result: Right(Person(Bridget Jones,29))</span>
* <br/>parsePerson(<span class="stQuotedString">"Bridget Jones"</span>, <span class="stQuotedString">""</span>)
* <span class="stLineComment">// Result: Left("" is not a valid integer)</span>
* <br/>parsePerson(<span class="stQuotedString">"Bridget Jones"</span>, <span class="stQuotedString">"-29"</span>)
* <span class="stLineComment">// Result: Left("-29" is not a valid age)</span>
* <br/>parsePerson(<span class="stQuotedString">""</span>, <span class="stQuotedString">""</span>)
* <span class="stLineComment">// Result: Left("" is not a valid name)</span>
* </pre>
*
* <h2>An <code>Either</code> with “attitude”</h2>
*
* <p>
* Because <code>Or</code> declares one alternative to be “good” and the other “bad,”
* it is more convenient than <code>Either</code> in this kind of situation. One difference to note with
* <code>Or</code> is that the <code>Good</code> alternative is on the left, <code>Bad</code> on the right.
* The reason is that <code>Or</code> is designed to be written using infix notation, and placing the
* “happy path” first is more readable. For example, instead of writing:
* </p>
*
* <pre class="stHighlighted">
* <span class="stType">Or[Int, ErrorMessage]</span>
* </pre>
*
* <p>
* You can write:
* </p>
*
* <pre class="stHighlighted">
* <span class="stType">Int</span> <span class="stType">Or</span> <span class="stType">ErrorMessage</span>
* </pre>
*
* <p>
* Here's how the <code>parseName</code> method might be written using an <code>Or</code>, where
* <code>ErrorMessage</code> is a type alias for <code>String</code> declared in the <code>org.scalactic</code>
* package object:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">import</span> org.scalactic._
* <br/><span class="stReserved">def</span> parseName(input: <span class="stType">String</span>): <span class="stType">String</span> <span class="stType">Or</span> <span class="stType">ErrorMessage</span> = {
* <span class="stReserved">val</span> trimmed = input.trim
* <span class="stReserved">if</span> (!trimmed.isEmpty) <span class="stType">Good</span>(trimmed) <span class="stReserved">else</span> <span class="stType">Bad</span>(s<span class="stQuotedString">""""${input}" is not a valid name"""</span>)
* }
* </pre>
*
* <p>
* You can think of the <code>String</code> <code>Or</code> <code>ErrorMessage</code> result
* type like this:
* </p>
*
* <blockquote>
* <em>The <code>parseName</code> method will return a name <code>String</code> or, if the input string
* is not a valid name, an <code>ErrorMessage</code>.</em>
* </blockquote>
*
* <p>
* Here's how the <code>parseAge</code> method might be written:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">def</span> parseAge(input: <span class="stType">String</span>): <span class="stType">Int</span> <span class="stType">Or</span> <span class="stType">ErrorMessage</span> = {
* <span class="stReserved">try</span> {
* <span class="stReserved">val</span> age = input.trim.toInt
* <span class="stReserved">if</span> (age >= <span class="stLiteral">0</span>) <span class="stType">Good</span>(age) <span class="stReserved">else</span> <span class="stType">Bad</span>(s<span class="stQuotedString">""""${age}" is not a valid age"""</span>)
* }
* <span class="stReserved">catch</span> {
* <span class="stReserved">case</span> _: <span class="stType">NumberFormatException</span> => <span class="stType">Bad</span>(s<span class="stQuotedString">""""${input}" is not a valid integer"""</span>)
* }
* }
* </pre>
*
* <p>
* Given these implementations, here's how you'd write the <code>parsePerson</code> method:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">def</span> parsePerson(inputName: <span class="stType">String</span>, inputAge: <span class="stType">String</span>): <span class="stType">Person</span> <span class="stType">Or</span> <span class="stType">ErrorMessage</span> =
* <span class="stReserved">for</span> {
* name <- parseName(inputName)
* age <- parseAge(inputAge)
* } <span class="stReserved">yield</span> <span class="stType">Person</span>(name, age)
* </pre>
*
* <p>
* Because of <code>Or</code>'s attitude, you need not write <code>.good</code> at the end of
* each generator. <code>Or</code> will keep going so long as each step produces a <code>Good</code>,
* short circuiting at the first sign of a <code>Bad</code>. Here are a few invocations of this
* <code>parsePerson</code> method:
* </p>
*
* <pre class="stHighlighted">
* parsePerson(<span class="stQuotedString">"Bridget Jones"</span>, <span class="stQuotedString">"29"</span>)
* <span class="stLineComment">// Result: Good(Person(Bridget Jones,29))</span>
* <br/>parsePerson(<span class="stQuotedString">"Bridget Jones"</span>, <span class="stQuotedString">""</span>)
* <span class="stLineComment">// Result: Bad("" is not a valid integer)</span>
* <br/>parsePerson(<span class="stQuotedString">"Bridget Jones"</span>, <span class="stQuotedString">"-29"</span>)
* <span class="stLineComment">// Result: Bad("-29" is not a valid age)</span>
* <br/>parsePerson(<span class="stQuotedString">""</span>, <span class="stQuotedString">""</span>)
* <span class="stLineComment">// Result: Bad("" is not a valid name)</span>
* </pre>
*
* <a name="accumulatingErrors"></a>
* <h2>Accumulating errors with <code>Or</code></h2>
*
* <p>
* Another difference between <code>Or</code> and <code>Either</code> is that <code>Or</code> enables
* you to accumulate errors if the <code>Bad</code> type is an <a href="Every.html"><code>Every</code></a>.
* An <code>Every</code> is similar to a <code>Seq</code> in that it contains ordered elements, but
* different from <code>Seq</code> in that it cannot be empty. An <code>Every</code> is
* either a <a href="One.html"><code>One</code></a>,
* which contains one and only one element, or a <a href="Many.html"><code>Many</code></a>, which contains two or
* more elements.
* </p>
*
* <p>
* <em>Note: an <code>Or</code> whose <code>Bad</code> type is an <code>Every</code>, or one of its subtypes,
* is called an “accumulating <code>Or</code>.”</em>
* </p>
*
* <p>
* To rewrite the previous example so that errors can be accumulated, you need first to return an <code>Every</code>
* as the <code>Bad</code> type. Here's how you'd change the <code>parseName</code> method:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">def</span> parseName(input: <span class="stType">String</span>): <span class="stType">String</span> <span class="stType">Or</span> <span class="stType">One[ErrorMessage]</span> = {
* <span class="stReserved">val</span> trimmed = input.trim
* <span class="stReserved">if</span> (!trimmed.isEmpty) <span class="stType">Good</span>(trimmed) <span class="stReserved">else</span> <span class="stType">Bad</span>(<span class="stType">One</span>(s<span class="stQuotedString">""""${input}" is not a valid name"""</span>))
* }
* </pre>
*
* <p>
* Because <code>parseName</code> will either return a valid name <code>String</code> wrapped in a
* <code>Good</code>, or <em>one</em> error message, wrapped in a <code>Bad</code>, you would write the
* <code>Bad</code> type as <code>One[ErrorMessage]</code>. The same is true for <code>parseAge</code>:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">def</span> parseAge(input: <span class="stType">String</span>): <span class="stType">Int</span> <span class="stType">Or</span> <span class="stType">One[ErrorMessage]</span> = {
* <span class="stReserved">try</span> {
* <span class="stReserved">val</span> age = input.trim.toInt
* <span class="stReserved">if</span> (age >= <span class="stLiteral">0</span>) <span class="stType">Good</span>(age) <span class="stReserved">else</span> <span class="stType">Bad</span>(<span class="stType">One</span>(s<span class="stQuotedString">""""${age}" is not a valid age"""</span>))
* }
* <span class="stReserved">catch</span> {
* <span class="stReserved">case</span> _: <span class="stType">NumberFormatException</span> => <span class="stType">Bad</span>(<span class="stType">One</span>(s<span class="stQuotedString">""""${input}" is not a valid integer"""</span>))
* }
* }
* </pre>
*
* <p>
* Because a <code>for</code> expression short-circuits on the first <code>Bad</code> encountered, you'll
* need to use a different approach to write the <code>parsePerson</code> method. In this example, the
* <code>withGood</code> method from trait <a href="Accumulation.html"><code>Accumulation</code></a>
* will do the trick:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">import</span> Accumulation._
* <br/><span class="stReserved">def</span> parsePerson(inputName: <span class="stType">String</span>, inputAge: <span class="stType">String</span>): <span class="stType">Person</span> <span class="stType">Or</span> <span class="stType">Every[ErrorMessage]</span> = {
* <span class="stReserved">val</span> name = parseName(inputName)
* <span class="stReserved">val</span> age = parseAge(inputAge)
* withGood(name, age) { <span class="stType">Person</span>(_, _) }
* }
* </pre>
*
* <p>
* Trait <code>Accumulation</code> offers overloaded <code>withGood</code> methods that take 1 to
* 22 accumulating <code>Or</code>s, plus a function taking the same number of corresponding
* <code>Good</code> values. In this example, if both <code>name</code> and <code>age</code> are
* <code>Good</code>s, the <code>withGood</code> method will pass the good name <code>String</code>
* and age <code>Int</code> to the <code>Person(_, _)</code> function, and return the resulting <code>Person</code>
* object wrapped in a <code>Good</code>. If either <code>name</code> and <code>age</code>, or both,
* are <code>Bad</code>, <code>withGood</code> will return the accumulated errors in a <code>Bad</code>.
* </p>
*
* <p>
* The result of <code>parsePerson</code>, if <code>Bad</code>, will therefore contain either one or two
* error messages, <em>i.e.</em>, the result will either be a <code>One</code> or a <code>Many</code>.
* As a result, the result type of <code>parsePerson</code> must be <code>Person</code> <code>Or</code>
* <code>Every[ErrorMessage]</code>. Regardless of whether a <code>Bad</code> result contains one
* or two error messages, it will contain <em>every</em> error message. Here's some invocations of
* this accumulating version of <code>parsePerson</code>:
* </p>
*
* <pre class="stHighlighted">
* parsePerson(<span class="stQuotedString">"Bridget Jones"</span>, <span class="stQuotedString">"29"</span>)
* <span class="stLineComment">// Result: Good(Person(Bridget Jones,29))</span>
* <br/>parsePerson(<span class="stQuotedString">"Bridget Jones"</span>, <span class="stQuotedString">""</span>)
* <span class="stLineComment">// Result: Bad(One("" is not a valid integer))</span>
* <br/>parsePerson(<span class="stQuotedString">"Bridget Jones"</span>, <span class="stQuotedString">"-29"</span>)
* <span class="stLineComment">// Result: Bad(One("-29" is not a valid age))</span>
* <br/>parsePerson(<span class="stQuotedString">""</span>, <span class="stQuotedString">""</span>)
* <span class="stLineComment">// Result: Bad(Many("" is not a valid name, "" is not a valid integer))</span>
* </pre>
*
* <p>
* Note that in the last example, the <code>Bad</code> contains an error message for both name and age.
* </p>
*
* <h2>Other ways to accumulate errors</h2>
* <p>
* The <code>Accumlation</code> trait also enables other ways of accumulating errors.
* </p>
*
* <a name="usingCombined"></a>
* <h3>Using <code>combined</code></h3>
*
* <p>
* If you have a collection of
* accumulating <code>Or</code>s, for example, you can <em>combine</em> them into one <code>Or</code> using <code>combined</code>, like this:
* </p>
*
* <pre class="stHighlighted">
* <span class="stType">List</span>(parseAge(<span class="stQuotedString">"29"</span>), parseAge(<span class="stQuotedString">"30"</span>), parseAge(<span class="stQuotedString">"31"</span>)).combined
* <span class="stLineComment">// Result: Good(List(29, 30, 31))</span>
* <br/><span class="stType">List</span>(parseAge(<span class="stQuotedString">"29"</span>), parseAge(<span class="stQuotedString">"-30"</span>), parseAge(<span class="stQuotedString">"31"</span>)).combined
* <span class="stLineComment">// Result: Bad(One("-30" is not a valid age))</span>
* <br/><span class="stType">List</span>(parseAge(<span class="stQuotedString">"29"</span>), parseAge(<span class="stQuotedString">"-30"</span>), parseAge(<span class="stQuotedString">"-31"</span>)).combined
* <span class="stLineComment">// Result: Bad(Many("-30" is not a valid age, "-31" is not a valid age))</span>
* </pre>
*
* <a name="usingValidatedBy"></a>
* <h3>Using <code>validatedBy</code></h3>
*
* <p>
* Or if you have a collection of values and a function that transforms that type of value into an accumulating
* <code>Or</code>s, you can validate the values using the function using <code>validatedBy</code>, like this:
* </p>
*
* <pre class="stHighlighted">
* <span class="stType">List</span>(<span class="stQuotedString">"29"</span>, <span class="stQuotedString">"30"</span>, <span class="stQuotedString">"31"</span>).validatedBy(parseAge)
* <span class="stLineComment">// Result: Good(List(29, 30, 31))</span>
* <br/><span class="stType">List</span>(<span class="stQuotedString">"29"</span>, <span class="stQuotedString">"-30"</span>, <span class="stQuotedString">"31"</span>).validatedBy(parseAge)
* <span class="stLineComment">// Result: Bad(One("-30" is not a valid age))</span>
* <br/><span class="stType">List</span>(<span class="stQuotedString">"29"</span>, <span class="stQuotedString">"-30"</span>, <span class="stQuotedString">"-31"</span>).validatedBy(parseAge)
* <span class="stLineComment">// Result: Bad(Many("-30" is not a valid age, "-31" is not a valid age))</span>
* </pre>
*
* <a name="usingZip"></a>
* <h3>Using <code>zip</code></h3>
*
* <p>
* You can also zip two accumulating <code>Or</code>s together. If both are <code>Good</code>, you'll get a
* <code>Good</code> tuple containin both original <code>Good</code> values. Otherwise, you'll get a <code>Bad</code>
* containing every error message. Here are some examples:
* </p>
*
* <pre class="stHighlighted">
* parseName(<span class="stQuotedString">"Dude"</span>) zip parseAge(<span class="stQuotedString">"21"</span>)
* <span class="stLineComment">// Result: Good((Dude,21))</span>
* <br/>parseName(<span class="stQuotedString">"Dude"</span>) zip parseAge(<span class="stQuotedString">"-21"</span>)
* <span class="stLineComment">// Result: Bad(One("-21" is not a valid age))</span>
* <br/>parseName(<span class="stQuotedString">""</span>) zip parseAge(<span class="stQuotedString">"-21"</span>)
* <span class="stLineComment">// Result: Bad(Many("" is not a valid name, "-21" is not a valid age))</span>
* </pre>
*
* <a name="usingWhen"></a>
* <h3>Using <code>when</code></h3>
*
* <p>
* In addition, given an accumlating <code>Or</code>, you can pass one or more <em>validation functions</em> to <code>when</code> on the <code>Or</code>
* to submit that <code>Or</code> to further scrutiny. A validation function accepts a <code>Good</code> type and returns a <code>Validation[E]</code>,
* where <code>E</code> is the type in the <code>Every</code> in the <code>Bad</code> type. For an <code>Int</code> <code>Or</code> <code>One[ErrorMessage]</code>, for example
* the validation function type would be <code>Int</code> <code>=></code> <code>Validation[ErrorMessage]</code>. Here are a few examples:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">def</span> isRound(i: <span class="stType">Int</span>): <span class="stType">Validation[ErrorMessage]</span> =
* <span class="stReserved">if</span> (i % <span class="stLiteral">10</span> == <span class="stLiteral">0</span>) <span class="stType">Pass</span> <span class="stReserved">else</span> <span class="stType">Fail</span>(i + <span class="stQuotedString">" was not a round number"</span>)
* <br/><span class="stReserved">def</span> isDivBy3(i: <span class="stType">Int</span>): <span class="stType">Validation[ErrorMessage]</span> =
* <span class="stReserved">if</span> (i % <span class="stLiteral">3</span> == <span class="stLiteral">0</span>) <span class="stType">Pass</span> <span class="stReserved">else</span> <span class="stType">Fail</span>(i + <span class="stQuotedString">" was not divisible by 3"</span>)
* </pre>
*
* <p>
* If the <code>Or</code> on which you call <code>when</code> is already <code>Bad</code>, you get the same (<code>Bad</code>) <code>Or</code> back, because
* no <code>Good</code> value exists to pass to the valiation functions:
* </p>
*
* <pre class="stHighlighted">
* parseAge(<span class="stQuotedString">"-30"</span>).when(isRound, isDivBy3)
* <span class="stLineComment">// Result: Bad(One("-30" is not a valid age))</span>
* </pre>
*
* <p>
* If the <code>Or</code> on which you call <code>when</code> is <code>Good</code>, and also passes all the validation functions (<em>i.e.</em>, the
* all return <code>None</code>), you again get the same <code>Or</code> back, but this time, a <code>Good</code> one:
* </p>
*
* <pre class="stHighlighted">
* parseAge(<span class="stQuotedString">"30"</span>).when(isRound, isDivBy3)
* <span class="stLineComment">// Result: Good(30)</span>
* </pre>
*
* <p>
* If one or more of the validation functions fails, however, you'll get a <code>Bad</code> back contining every error. Here are some examples:
* </p>
*
* <pre class="stHighlighted">
* parseAge(<span class="stQuotedString">"33"</span>).when(isRound, isDivBy3)
* <span class="stLineComment">// Result: Bad(One(33 was not a round number))</span>
* <br/>parseAge(<span class="stQuotedString">"20"</span>).when(isRound, isDivBy3)
* <span class="stLineComment">// Result: Bad(One(20 was not divisible by 3))</span>
* <br/>parseAge(<span class="stQuotedString">"31"</span>).when(isRound, isDivBy3)
* <span class="stLineComment">// Result: Bad(Many(31 was not a round number, 31 was not divisible by 3))</span>
* </pre>
*
* <p>
* Note that you can use <code>when</code> to accumulate errors in a <code>for</code> expression involving an accumulating <code>Or</code>, like this:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">for</span> (age <- parseAge(<span class="stQuotedString">"-30"</span>) when (isRound, isDivBy3)) <span class="stReserved">yield</span> age
* <span class="stLineComment">// Result: Bad(One("-30" is not a valid age))</span>
* <br/><span class="stReserved">for</span> (age <- parseAge(<span class="stQuotedString">"30"</span>) when (isRound, isDivBy3)) <span class="stReserved">yield</span> age
* <span class="stLineComment">// Result: Good(30)</span>
* <br/><span class="stReserved">for</span> (age <- parseAge(<span class="stQuotedString">"33"</span>) when (isRound, isDivBy3)) <span class="stReserved">yield</span> age
* <span class="stLineComment">// Result: Bad(One(33 was not a round number))</span>
* <br/><span class="stReserved">for</span> (age <- parseAge(<span class="stQuotedString">"20"</span>) when (isRound, isDivBy3)) <span class="stReserved">yield</span> age
* <span class="stLineComment">// Result: Bad(One(20 was not divisible by 3))</span>
* <br/><span class="stReserved">for</span> (age <- parseAge(<span class="stQuotedString">"31"</span>) when (isRound, isDivBy3)) <span class="stReserved">yield</span> age
* <span class="stLineComment">// Result: Bad(Many(31 was not a round number, 31 was not divisible by 3))</span>
* </pre>
*
* <h2>Much ado about <code>Nothing</code></h2>
*
* <p>
* Because <code>Or</code> has two types, but each of its two subtypes only takes a value of one or the other type, the Scala compiler will
* infer <code>Nothing</code> for the unspecified type:
* </p>
*
* <pre class="stREPL">
* scala> Good(3)
* res0: org.scalactic.Good[Int,Nothing] = Good(3)
*
* scala> Bad("oops")
* res1: org.scalactic.Bad[Nothing,String] = Bad(oops)
* </pre>
*
* <p>
* Often <code>Nothing</code> will work fine, as it will be widened as soon as the compiler encounters a more specific type.
* Sometimes, however, you may need to specify it. In such situations you can use this syntax:
* </p>
*
* <pre class="stREPL">
* scala> Good(3).orBad[String]
* res2: org.scalactic.Good[Int,String] = Good(3)
*
* scala> Good[Int].orBad("oops")
* res3: org.scalactic.Bad[Int,String] = Bad(oops)
* </pre>
*
* <p>
* If you want to specify both types, because you don't like the inferred type, you can do so like this:
* </p>
*
* <pre class="stREPL">
* scala> Good[AnyVal, String](3)
* res4: org.scalactic.Good[AnyVal,String] = Good(3)
*
* scala> Bad[Int, ErrorMessage]("oops")
* res5: org.scalactic.Bad[Int,org.scalactic.ErrorMessage] = Bad(oops)
* </pre>
*
* <p>
* But you may find the code is clearer if you instead use a type ascription, like this:
* </p>
*
* <pre class="stREPL">
* scala> Good(3): AnyVal Or String
* res6: org.scalactic.Or[AnyVal,String] = Good(3)
*
* scala> Bad("oops"): Int Or ErrorMessage
* res7: org.scalactic.Or[Int,org.scalactic.ErrorMessage] = Bad(oops)
* </pre>
*
* <p>
* <em>Note: The <code>Or</code> hierarchy was inspired in part by the disjoint union (<code>\/</code>) and <code>Validation</code> types of
* <a href="http://code.google.com/p/scalaz/" target="_blank"><code>scalaz</code></a>, the <code>ProcessResult</code> type of
* <a href="https://github.com/typesafehub/activator/">Typesafe Activator</a>, and the <code>Result</code> type of
* <a href="https://github.com/vpatryshev/ScalaKittens">ScalaKittens</a>.</em>
* </p>
*/
sealed abstract class Or[+G,+B] extends Product with Serializable {
/**
* Indicates whether this <code>Or</code> is a <code>Good</code>
*
* @return true if this <code>Or</code> is a <code>Good</code>, <code>false</code> if it is a <code>Bad</code>.
*/
val isGood: Boolean = false
/**
* Indicates whether this <code>Or</code> is a <code>Bad</code>
*
* @return true if this <code>Or</code> is a <code>Bad</code>, <code>false</code> if it is a <code>Good</code>.
*/
val isBad: Boolean = false
/**
* Returns the <code>Or</code>'s value if it is a <code>Good</code> or throws <code>NoSuchElementException</code> if it is a <code>Bad</code>.
*
* @return the contained value if this is a <code>Good</code>
* @throws NoSuchElementException if this is a <code>Bad</code>
*/
def get: G
/**
* Maps the given function to this <code>Or</code>'s value if it is a <code>Good</code> or returns <code>this</code> if it is a <code>Bad</code>.
*
* @param f the function to apply
* @return if this is a <code>Good</code>, the result of applying the given function to the contained value wrapped in a <code>Good</code>,
* else this <code>Bad</code> is returned
*/
def map[H](f: G => H): H Or B
/**
* Maps the given function to this <code>Or</code>'s value if it is a <code>Bad</code> or returns <code>this</code> if it is a <code>Good</code>.
*
* @param f the function to apply
* @return if this is a <code>Bad</code>, the result of applying the given function to the contained value wrapped in a <code>Bad</code>,
* else this <code>Good</code> is returned
*/
def badMap[C](f: B => C): G Or C
/**
* Maps the given function to this <code>Or</code>'s value if it is a <code>Bad</code>, transforming it into a <code>Good</code>, or returns
* <code>this</code> if it is already a <code>Good</code>.
*
* @param f the function to apply
* @return if this is a <code>Bad</code>, the result of applying the given function to the contained value wrapped in a <code>Good</code>,
* else this <code>Good</code> is returned
*/
def recover[H >: G](f: B => H): H Or B
/**
* Maps the given function to this <code>Or</code>'s value if it is a <code>Bad</code>, returning the result, or returns
* <code>this</code> if it is already a <code>Good</code>.
*
* @param f the function to apply
* @return if this is a <code>Bad</code>, the result of applying the given function to the contained value,
* else this <code>Good</code> is returned
*/
def recoverWith[H >: G, C](f: B => H Or C): H Or C
/**
* Applies the given function f to the contained value if this <code>Or</code> is a <code>Good</code>; does nothing if this <code>Or</code>
* is a <code>Bad</code>.
*
* @param f the function to apply
*/
def foreach(f: G => Unit): Unit
/**
* Returns the given function applied to the value contained in this <code>Or</code> if it is a <code>Good</code>,
* or returns <code>this</code> if it is a <code>Bad</code>.
*
* @param f the function to apply
* @return if this is a <code>Good</code>, the result of applying the given function to the contained value wrapped in a <code>Good</code>,
* else this <code>Bad</code> is returned
*/
def flatMap[H, C >: B](f: G => H Or C): H Or C
/**
* Returns this <code>Or</code> if either 1) it is a <code>Bad</code> or 2) it is a <code>Good</code> and applying the validation function <code>f</code> to this
* <code>Good</code>'s value returns <code>Pass</code>; otherwise,
* returns a new <code>Bad</code> containing the error value contained in the <code>Fail</code> resulting from applying the validation
* function <code>f</code> to this <code>Good</code>'s value.
*
* <p>
* For examples of <code>filter</code> used in <code>for</code> expressions, see the main documentation for trait
* <a href="Validation.html"><code>Validation</code></a>.
* </p>
*
* @param f the validation function to apply
* @return a <code>Good</code> if this <code>Or</code> is a <code>Good</code> that passes the validation function, else a <code>Bad</code>.
*/
def filter[C >: B](f: G => Validation[C]): G Or C
// TODO: What should we do about withFilter. Good question for the hackathon.
/**
* Currently just forwards to </code>filter</code>, and therefore, returns the same result.
*/
def withFilter[C >: B](f: G => Validation[C]): G Or C = filter(f)
/**
* Returns <code>true</code> if this <code>Or</code> is a <code>Good</code> and the predicate <code>p</code> returns true when applied to this <code>Good</code>'s value.
*
* <p>
* Note: The <code>exists</code> method will return the same result as <code>forall</code> if this <code>Or</code> is a <code>Good</code>, but the opposite
* result if this <code>Or</code> is a <code>Bad</code>.
* </p>
*
* @param p the predicate to apply to the <code>Good</code> value, if this is a <code>Good</code>
* @return the result of applying the passed predicate <code>p</code> to the <code>Good</code> value, if this is a <code>Good</code>, else <code>false</code>
*/
def exists(p: G => Boolean): Boolean
/**
* Returns <code>true</code> if either this <code>Or</code> is a <code>Bad</code> or if the predicate <code>p</code> returns <code>true</code> when applied
* to this <code>Good</code>'s value.
*
* <p>
* Note: The <code>forall</code> method will return the same result as <code>exists</code> if this <code>Or</code> is a <code>Good</code>, but the opposite
* result if this <code>Or</code> is a <code>Bad</code>.
* </p>
*
* @param p the predicate to apply to the <code>Good</code> value, if this is a <code>Good</code>
* @return the result of applying the passed predicate <code>p</code> to the <code>Good</code> value, if this is a <code>Good</code>, else <code>true</code>
*/
def forall(f: G => Boolean): Boolean
/**
* Returns, if this <code>Or</code> is <code>Good</code>, this <code>Good</code>'s value; otherwise returns the result of evaluating <code>default</code>.
*
* @param default the default expression to evaluate if this <code>Or</code> is a <code>Bad</code>
* @return the contained value, if this <code>Or</code> is a <code>Good</code>, else the result of evaluating the given <code>default</code>
*/
def getOrElse[H >: G](default: => H): H
/**
* Returns this <code>Or</code> if it is a <code>Good</code>, otherwise returns the result of evaluating the passed <code>alternative</code>.
*
* @param alternative the alternative by-name to evaluate if this <code>Or</code> is a <code>Bad</code>
* @return this <code>Or</code>, if it is a <code>Good</code>, else the result of evaluating <code>alternative</code>
*/
def orElse[H >: G, C >: B](alternative: => H Or C): H Or C
/**
* Returns a <code>Some</code> containing the <code>Good</code> value, if this <code>Or</code> is a <code>Good</code>, else <code>None</code>.
*
* @return the contained “good” value wrapped in a <code>Some</code>, if this <code>Or</code> is a <code>Good</code>; <code>None</code>
* if this <code>Or</code> is a <code>Bad</code>.
*/
def toOption: Option[G]
/**
* Returns an immutable <code>IndexedSeq</code> containing the <code>Good</code> value, if this <code>Or</code> is a <code>Good</code>, else an empty
* immutable <code>IndexedSeq</code>.
*
* @return the contained “good” value in a lone-element <code>Seq</code> if this <code>Or</code> is a <code>Good</code>; an empty <code>Seq</code> if
* this <code>Or</code> is a <code>Bad</code>.
*/
def toSeq: scala.collection.immutable.IndexedSeq[G]
/**
* Returns an <code>Either</code>: a <code>Right</code> containing the <code>Good</code> value, if this is a <code>Good</code>; a <code>Left</code>
* containing the <code>Bad</code> value, if this is a <code>Bad</code>.
*
* <p>
* Note that values effectively “switch sides” when convering an <code>Or</code> to an <code>Either</code>. If the type of the
* <code>Or</code> on which you invoke <code>toEither</code> is <code>Or[Int, ErrorMessage]</code> for example, the result will be an
* <code>Either[ErrorMessage, Int]</code>. The reason is that the convention for <code>Either</code> is that <code>Left</code> is used for “bad”
* values and <code>Right</code> is used for “good” ones.
* </p>
*
* @return this <code>Good</code> value, wrapped in a <code>Right</code>, or this <code>Bad</code> value, wrapped in a <code>Left</code>.
*/
def toEither: Either[B, G]
/**
* Converts this <code>Or</code> to an <code>Or</code> with the same <code>Good</code> type and a <code>Bad</code> type consisting of
* <a href="One.html"><code>One</code></a> parameterized by this <code>Or</code>'s <code>Bad</code> type.
*
* <p>
* For example, invoking the <code>accumulating</code> method on an <code>Int Or ErrorMessage</code> would convert it to an
* <code>Int Or One[ErrorMessage]</code>. This result type, because the <code>Bad</code> type is an <code>Every</code>, can be used
* with the mechanisms provided in trait <a href="Accumulation.html"><code>Accumulation</code></a> to accumulate errors.
* <p>
*
* <p>
* Note that if this <code>Or</code> is already an accumulating <code>Or</code>, the behavior of this <code>accumulating</code> method does not change.
* For example, if you invoke <code>accumulating</code> on an <code>Int Or One[ErrorMessage]</code> you will be rewarded with an
* <code>Int Or One[One[ErrorMessage]]</code>.
* </p>
*
* @return this <code>Good</code>, if this <code>Or</code> is a <code>Good</code>; or this <code>Bad</code> value wrapped in a <code>One</code> if
* this <code>Or</code> is a <code>Bad</code>.
*/
def accumulating: G Or One[B]
/**
* Returns a <code>Try</code>: a <code>Success</code> containing the
* <code>Good</code> value, if this is a <code>Good</code>; a <code>Failure</code>
* containing the <code>Bad</code> value, if this is a <code>Bad</code>.
*
* <p>
* Note: This method can only be called if the <code>Bad</code> type of this <code>Or</code> is a subclass
* of <code>Throwable</code> (or <code>Throwable</code> itself).
* </p>
*
* <p>
* Note that values effectively “switch sides” when converting an <code>Or</code> to an <code>Either</code>. If the type of the
* <code>Or</code> on which you invoke <code>toEither</code> is <code>Or[Int, ErrorMessage]</code> for example, the result will be an
* <code>Either[ErrorMessage, Int]</code>. The reason is that the convention for <code>Either</code> is that <code>Left</code> is used for “bad”
* values and <code>Right</code> is used for “good” ones.
* </p>
*
* @return this <code>Good</code> value, wrapped in a <code>Right</code>, or this <code>Bad</code> value, wrapped in a <code>Left</code>.
*/
def toTry(implicit ev: B <:< Throwable): Try[G]
/**
* Returns an <code>Or</code> with the <code>Good</code> and <code>Bad</code> types swapped: <code>Bad</code> becomes <code>Good</code> and <code>Good</code>
* becomes <code>Bad</code>.
*
* <p>
* Here's an example:
* </p>
*
* <pre class="stREPL">
* scala> val lyrics = Bad("Hey Jude, don't make it bad. Take a sad song and make it better.")
* lyrics: org.scalactic.Bad[Nothing,String] =
* Bad(Hey Jude, don't make it bad. Take a sad song and make it better.)
*
* scala> lyrics.swap
* res12: org.scalactic.Or[String,Nothing] =
* Good(Hey Jude, don't make it bad. Take a sad song and make it better.)
* </pre>
*
* <p>
* Now that song will be rolling around in your head all afternoon. But at least it is a good song (thanks to <code>swap</code>).
* </p>
*
* @return if this <code>Or</code> is a <code>Good</code>, its <code>Good</code> value wrapped in a <code>Bad</code>; if this <code>Or</code> is
* a <code>Bad</code>, its <code>Bad</code> value wrapped in a <code>Good</code>.
*/
def swap: B Or G
/**
* Transforms this <code>Or</code> by applying the function <code>gf</code> to this <code>Or</code>'s <code>Good</code> value if it is a <code>Good</code>,
* or by applying <code>bf</code> to this <code>Or</code>'s <code>Bad</code> value if it is a <code>Bad</code>.
*
* @param gf the function to apply to this <code>Or</code>'s <code>Good</code> value, if it is a <code>Good</code>
* @param bf the function to apply to this <code>Or</code>'s <code>Bad</code> value, if it is a <code>Bad</code>
* @return the result of applying the appropriate one of the two passed functions, <code>gf</code> or </code>bf</code>, to this <code>Or</code>'s value
*/
def transform[H, C](gf: G => H Or C, bf: B => H Or C): H Or C
/**
* Folds this <code>Or</code> into a value of type <code>V</code> by applying the given <code>gf</code> function if this is
* a <code>Good</code> else the given <code>bf</code> function if this is a <code>Bad</code>.
*
* @param gf the function to apply to this <code>Or</code>'s <code>Good</code> value, if it is a <code>Good</code>
* @param bf the function to apply to this <code>Or</code>'s <code>Bad</code> value, if it is a <code>Bad</code>
* @return the result of applying the appropriate one of the two passed functions, <code>gf</code> or </code>bf</code>, to this <code>Or</code>'s value
*/
def fold[V](gf: G => V, bf: B => V): V
/**
* <strong>The <code>asOr</code> method has been deprecated and will be removed in a future version of Scalactic.
* Please remove invocations of <code>asOr</code> in expressions of type <code>Good(value).orBad[Type]</code> and
* <code>Good[Type].orBad(value)</code> (which now return a type already widened to <code>Or</code>), otherwise please
* use a type annotation to widen the type, such as: <code>(Good(3): Int Or ErrorMessage)</code>.</strong>
*/
@deprecated("The asOr is no longer needed because Good(value).orBad[Type] and Good[Type].orBad(value) now return Or. You can delete invocations of asOr in those cases, otherwise, please use a type annotation to widen the type, like (Good(3): Int Or ErrorMessage).")
def asOr: G Or B = this
}
/**
* The companion object for <code>Or</code> providing factory methods for creating <code>Or</code>s from <code>Either</code>s and <code>Try</code>s.
*/
object Or {
/**
* Trait providing a concise <em>type lambda</em> syntax for <code>Or</code> types partially applied on their "bad" type.
*
* <p>
* This trait is used to curry the type parameters of <code>Or</code>, which takes two type parameters,
* into a type (this trait) which takes one parameter, and another (its type member) which
* takes the other. For example, type <code>Or[GOOD, BAD]</code> (which can be written in infix form
* as <code>GOOD Or BAD</code>) can be expressed in curried form as <code>Or.B[BAD]#G[GOOD]</code>.
* Leaving off the final <code>GOOD</code> type parameter yields a "type lambda," such as <code>Or.B[ErrorMessage]#G</code>.
* </p>
*
* <p>
* For example, consider this method that takes two type parameters, a <em>type constructor</em> named <code>Context</code> and a
* type named <code>A</code>:
* </p>
*
* <pre>
* scala> def example[Context[_], A](ca: Context[A]) = ca
* example: [Context[_], A](ca: Context[A])Context[A]
* </pre>
*
* <p>
* Because <code>List</code> takes a single type parameter, it fits the shape of <code>Context</code>,
* it can be simply passed to <code>example</code>--<em>i.e.</em>, the compiler will infer <code>Context</code> as <code>List</code>:
* </p>
*
* <pre>
* scala> example(List(1, 2, 3))
* res0: List[Int] = List(1, 2, 3)
* </pre>
*
* <p>
* But because <code>Or</code> takes two type parameters, <code>G</code> for the "good" type and <code>B</code> for the "bad" type, it
* cannot simply be passed, because the compiler doesn't know which of <code>G</code> or </code>B</code> you'd want to abstract over:
* </p>
*
* <pre>
* scala> val or: Int Or ErrorMessage = Good(3)
* or: org.scalactic.Or[Int,org.scalactic.ErrorMessage] = Good(3)
*
* scala> example(or)
* <console>:16: error: no type parameters for method example: (ca: Context[A])Context[A] exist so that it can be applied to arguments (org.scalactic.Or[Int,org.scalactic.ErrorMessage])
* --- because ---
* argument expression's type is not compatible with formal parameter type;
* found : org.scalactic.Or[Int,org.scalactic.ErrorMessage]
* (which expands to) org.scalactic.Or[Int,String]
* required: ?Context[?A]
* example(or)
* ^
* <console>:16: error: type mismatch;
* found : org.scalactic.Or[Int,org.scalactic.ErrorMessage]
* (which expands to) org.scalactic.Or[Int,String]
* required: Context[A]
* example(or)
* ^
* </pre>
*
* <p>
* You must therefore tell the compiler which one you want with a "type lambda." Here's an example:
* </p>
*
* <pre>
* scala> example[({type L[G] = G Or ErrorMessage})#L, Int](or)
* res1: org.scalactic.Or[Int,org.scalactic.ErrorMessage] = Good(3)
* </pre>
*
* <p>
* The alternate type lambda syntax provided by this trait is more concise and hopefully easier to remember and read:
* </p>
*
* <pre>
* scala> example[Or.B[ErrorMessage]#G, Int](or)
* res2: org.scalactic.Or[Int,org.scalactic.ErrorMessage] = Good(3)
* </pre>
*
* <p>
* You can read <code>Or.B[ErrorMessage]#G</code> as: an <code>Or</code> with its "bad" type, <code>B</code>,
* fixed to <code>ErrorMessage</code> and its "good" type, <code>G</code>, left unspecified.
* </p>
*/
private[scalactic] trait B[BAD] {
/**
* Type member that provides a curried alias to <code>G</code> <code>Or</code> <code>B</code>.
*
* <p>
* See the main documentation for trait <code>B</code> for more detail.
* </p>
*/
type G[GOOD] = GOOD Or BAD
}
/**
* Trait providing a concise <em>type lambda</em> syntax for <code>Or</code> types partially applied on their "good" type.
*
* <p>
* This trait is used to curry the type parameters of <code>Or</code>, which takes two type parameters,
* into a type (this trait) which takes one parameter, and another (its type member) which
* takes the other. For example, type <code>Or[GOOD, BAD]</code> (which can be written in infix form
* as <code>GOOD Or BAD</code>) can be expressed in curried form as <code>Or.G[GOOD]#B[BAD]</code>.
* Leaving off the final <code>B</code> type parameter yields a "type lambda," such as <code>Or.G[Int]#B</code>.
* </p>
*
* <p>
* For example, consider this method that takes two type parameters, a <em>type constructor</em> named <code>Context</code> and a
* type named <code>A</code>:
* </p>
*
* <pre>
* scala> def example[Context[_], A](ca: Context[A]) = ca
* example: [Context[_], A](ca: Context[A])Context[A]
* </pre>
*
* <p>
* Because <code>List</code> takes a single type parameter, it fits the shape of <code>Context</code>,
* it can be simply passed to <code>example</code>--<em>i.e.</em>, the compiler will infer <code>Context</code> as <code>List</code>:
* </p>
*
* <pre>
* scala> example(List(1, 2, 3))
* res0: List[Int] = List(1, 2, 3)
* </pre>
*
* <p>
* But because <code>Or</code> takes two type parameters, <code>G</code> for the "good" type and <code>B</code> for the "bad" type, it
* cannot simply be passed, because the compiler doesn't know which of <code>G</code> or </code>B</code> you'd want to abstract over:
* </p>
*
* <pre>
* scala> val or: Int Or ErrorMessage = Good(3)
* or: org.scalactic.Or[Int,org.scalactic.ErrorMessage] = Good(3)
*
* scala> example(or)
* <console>:16: error: no type parameters for method example: (ca: Context[A])Context[A] exist so that it can be applied to arguments (org.scalactic.Or[Int,org.scalactic.ErrorMessage])
* --- because ---
* argument expression's type is not compatible with formal parameter type;
* found : org.scalactic.Or[Int,org.scalactic.ErrorMessage]
* (which expands to) org.scalactic.Or[Int,String]
* required: ?Context[?A]
* example(or)
* ^
* <console>:16: error: type mismatch;
* found : org.scalactic.Or[Int,org.scalactic.ErrorMessage]
* (which expands to) org.scalactic.Or[Int,String]
* required: Context[A]
* example(or)
* ^
* </pre>
*
* <p>
* You must therefore tell the compiler which one you want with a "type lambda." Here's an example:
* </p>
*
* <pre>
* scala> example[({type L[B] = Int Or B})#L, ErrorMessage](or)
* res1: org.scalactic.Or[Int,org.scalactic.ErrorMessage] = Good(3)
* </pre>
*
* <p>
* The alternate type lambda syntax provided by this trait is more concise and hopefully easier to remember and read:
* </p>
*
* <pre>
* scala> example[Or.G[Int]#B, ErrorMessage](or)
* res15: org.scalactic.Or[Int,org.scalactic.ErrorMessage] = Good(3)
* </pre>