@@ -3,6 +3,7 @@ package demos
33@ munit.IgnoreSuite
44class EitherDemo extends munit.FunSuite {
55
6+ import cats .syntax .applicativeError ._
67 import cats .syntax .either ._
78
89 test(" creation" ) {
@@ -42,30 +43,16 @@ class EitherDemo extends munit.FunSuite {
4243 }
4344
4445 test(" error handling" ) {
45-
46- def handle (e : Either [String , Int ]): Either [String , Int ] =
47- e.recover(e =>
48- e match {
49- case " unknown" => 42
50- }
51- )
46+ val e1 : Either [String , Int ] = Left (" unknown" )
5247
5348 // The Left[String] value is converted to a Right[Int]
54- val e1 : Either [String , Int ] = handle(Left (" unknown" ))
55-
56- // The Left[String] value isn't converted
57- val e2 : Either [String , Int ] = handle(Left (" anything else" ))
49+ val e2 : Either [String , Int ] =
50+ e1.handleError(e => e.hashCode())
5851
59- def handleWith (e : Either [String , Int ]): Either [String , Int ] =
60- e.recoverWith(e =>
61- e match {
62- case " unknown" => Right (42 )
63- }
64- )
52+ val e3 : Either [String , Int ] = Left (" unknown" )
6553
6654 // Same conversions as before
67- val e3 : Either [String , Int ] = handleWith(Left (" unknown" ))
68- val e4 : Either [String , Int ] = handleWith(Left (" anything else" ))
55+ val e4 : Either [String , Int ] = e3.handleErrorWith(e => Right (e.hashCode))
6956 }
7057
7158}
0 commit comments