Skip to content

Commit

Permalink
Merge pull request #2119 from fcanedo/fix-optiont-getorelse
Browse files Browse the repository at this point in the history
Fixes type params for OptionT.getOrElse(F)
  • Loading branch information
ceedubs authored Jan 22, 2018
2 parents 8db3501 + cb2aba4 commit 32bcfc5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions core/src/main/scala/cats/data/OptionT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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] =
Expand Down
11 changes: 11 additions & 0 deletions tests/src/test/scala/cats/tests/OptionTSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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("error".asLeft[Int]))
}
}

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("error".asLeft[Int]))
}
}
test("OptionT[Id, A].collect consistent with Option.collect") {
forAll { (o: Option[Int], f: Int => Option[String]) =>
val p = Function.unlift(f)
Expand Down

0 comments on commit 32bcfc5

Please sign in to comment.