From 7ccaa4f7aeb94559332ba6c28fd0344b9671da99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Jos=C3=A9=20Canedo=20Dominguez?= Date: Sun, 17 Dec 2017 22:20:31 +0100 Subject: [PATCH 1/3] Fixes type params for OptionT.getOrElse(F) Adds a type parameter to getOrElse and getOrElseF to conform to Option.getOrElse, which allows it to be called without needing a type hint: OptionT(Future { Option(Right(1)) }).getOrElse(Left("error")) Instead of: OptionT(Future { Option(Right(1): Either[String, Int]) }).getOrElse(Left("error")) --- core/src/main/scala/cats/data/OptionT.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/scala/cats/data/OptionT.scala b/core/src/main/scala/cats/data/OptionT.scala index df24420c1b..9a51b49638 100644 --- a/core/src/main/scala/cats/data/OptionT.scala +++ b/core/src/main/scala/cats/data/OptionT.scala @@ -51,10 +51,10 @@ final case class OptionT[F[_], A](value: F[Option[A]]) { def subflatMap[B](f: A => Option[B])(implicit F: Functor[F]): OptionT[F, B] = transform(_.flatMap(f)) - def getOrElse(default: => A)(implicit F: Functor[F]): F[A] = + def getOrElse[B >: A](default: => B)(implicit F: Functor[F]): F[B] = F.map(value)(_.getOrElse(default)) - def getOrElseF(default: => F[A])(implicit F: Monad[F]): F[A] = + def getOrElseF[B >: A](default: => F[B])(implicit F: Monad[F]): F[B] = F.flatMap(value)(_.fold(default)(F.pure)) def collect[B](f: PartialFunction[A, B])(implicit F: Functor[F]): OptionT[F, B] = From a9c118e58712cf25e0986b0f5ccd585bdf1da977 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Jos=C3=A9=20Canedo=20Dominguez?= Date: Thu, 21 Dec 2017 11:06:18 +0100 Subject: [PATCH 2/3] Adds some tests --- tests/src/test/scala/cats/tests/OptionTSuite.scala | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/src/test/scala/cats/tests/OptionTSuite.scala b/tests/src/test/scala/cats/tests/OptionTSuite.scala index d3b7eea447..8e9388045e 100644 --- a/tests/src/test/scala/cats/tests/OptionTSuite.scala +++ b/tests/src/test/scala/cats/tests/OptionTSuite.scala @@ -148,12 +148,23 @@ class OptionTSuite extends CatsSuite { } } + test("OptionT[Id, A].getOrElse consistent with Option.getOrElse, with respect to types") { + forAll { (o: Option[Int]) => + o.map(Right.apply).getOrElse(Left("error")) should === (OptionT[Id, Int](o).map(Right.apply).getOrElse(Left("error"))) + } + } + test("OptionT[Id, A].getOrElseF consistent with Option.getOrElse") { forAll { (o: Option[Int], i: Int) => o.getOrElse(i) should === (OptionT[Id, Int](o).getOrElseF(i)) } } + test("OptionT[Id, A].getOrElseF consistent with Option.getOrElse, with respect to types") { + forAll { (o: Option[Int]) => + o.map(Right.apply).getOrElse(Left("error")) should === (OptionT[Id, Int](o).map(Right.apply).getOrElseF(Left("error"))) + } + } test("OptionT[Id, A].collect consistent with Option.collect") { forAll { (o: Option[Int], f: Int => Option[String]) => val p = Function.unlift(f) From cb2aba492bac87d5556be08c8518755dbbbe4e99 Mon Sep 17 00:00:00 2001 From: "Kai(luo) Wang" Date: Fri, 12 Jan 2018 20:45:30 -0500 Subject: [PATCH 3/3] trying to fix build for 2.11- --- tests/src/test/scala/cats/tests/OptionTSuite.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/src/test/scala/cats/tests/OptionTSuite.scala b/tests/src/test/scala/cats/tests/OptionTSuite.scala index 8e9388045e..32e3458fda 100644 --- a/tests/src/test/scala/cats/tests/OptionTSuite.scala +++ b/tests/src/test/scala/cats/tests/OptionTSuite.scala @@ -150,7 +150,7 @@ class OptionTSuite extends CatsSuite { test("OptionT[Id, A].getOrElse consistent with Option.getOrElse, with respect to types") { forAll { (o: Option[Int]) => - o.map(Right.apply).getOrElse(Left("error")) should === (OptionT[Id, Int](o).map(Right.apply).getOrElse(Left("error"))) + o.map(Right.apply).getOrElse(Left("error")) should === (OptionT[Id, Int](o).map(Right.apply).getOrElse("error".asLeft[Int])) } } @@ -162,7 +162,7 @@ class OptionTSuite extends CatsSuite { test("OptionT[Id, A].getOrElseF consistent with Option.getOrElse, with respect to types") { forAll { (o: Option[Int]) => - o.map(Right.apply).getOrElse(Left("error")) should === (OptionT[Id, Int](o).map(Right.apply).getOrElseF(Left("error"))) + o.map(Right.apply).getOrElse(Left("error")) should === (OptionT[Id, Int](o).map(Right.apply).getOrElseF("error".asLeft[Int])) } } test("OptionT[Id, A].collect consistent with Option.collect") {