chapter 4 exercise 6 (4.6)

17 views
Skip to first unread message

mohamedal...@gmail.com

unread,
Jun 22, 2017, 5:50:19 PM6/22/17
to scala-functional
The compiler is fine even without using the covariance. Then why we have to use the covariance like "def flatMap[EE >: E, B](f: A => Either[EE, B]): Either[EE, B]" as mentioned in the book.

def flatMap[E, B](f: A => Either[E, B]): Either[E, B] = this match {

case Left(e:E) => Left(e)
case Right(a:A) => f(a)
}

def orElse[ E, A](b: => Either[E, A]) : Either[E, A] = this match {
case Left(e:E) => b
case Right(a:A) => Right(a)
}

Please let me know why the above works. Please give me an example where it fails if I don't use the covariance notion as mentioned in the book.

Thanks & Regards,
Mohamed

mohamedal...@gmail.com

unread,
Jun 22, 2017, 6:01:06 PM6/22/17
to scala-functional
Forgot to include the Scala version. The version is 2.12.2

Reply all
Reply to author
Forward
0 new messages