From b43ab251ef687edb5ec098df4f79c16acb7fdf47 Mon Sep 17 00:00:00 2001 From: QueenJcloud Date: Fri, 10 Oct 2025 18:59:02 +0100 Subject: [PATCH 1/3] Add doctest example for OptionT --- core/src/main/scala/cats/data/OptionT.scala | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/core/src/main/scala/cats/data/OptionT.scala b/core/src/main/scala/cats/data/OptionT.scala index e900783d60..26c1be03ed 100644 --- a/core/src/main/scala/cats/data/OptionT.scala +++ b/core/src/main/scala/cats/data/OptionT.scala @@ -30,6 +30,20 @@ package data * * For more information, see the [[http://typelevel.org/cats/datatypes/optiont.html documentation]]. */ + /** + * OptionT is a wrapper for computations of type F[Option[A]]. + * + * Example: + * {{{ + * import cats.data.OptionT + * import cats.implicits._ + * + * val result = OptionT(List(Option(1), None, Option(3))) + * result.map(_ + 1) + * // res0: cats.data.OptionT[List, Int] = OptionT(List(Some(2), None, Some(4))) + * }}} + */ + final case class OptionT[F[_], A](value: F[Option[A]]) { def fold[B](default: => B)(f: A => B)(implicit F: Functor[F]): F[B] = From 4fefd78b36e697a0d14807aaf9a47ae9266c7648 Mon Sep 17 00:00:00 2001 From: QueenJcloud Date: Sat, 11 Oct 2025 16:12:54 +0100 Subject: [PATCH 2/3] Updated OptionT ScalaDoc: merged example into existing comment as suggested --- core/src/main/scala/cats/data/OptionT.scala | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/core/src/main/scala/cats/data/OptionT.scala b/core/src/main/scala/cats/data/OptionT.scala index 26c1be03ed..e900783d60 100644 --- a/core/src/main/scala/cats/data/OptionT.scala +++ b/core/src/main/scala/cats/data/OptionT.scala @@ -30,20 +30,6 @@ package data * * For more information, see the [[http://typelevel.org/cats/datatypes/optiont.html documentation]]. */ - /** - * OptionT is a wrapper for computations of type F[Option[A]]. - * - * Example: - * {{{ - * import cats.data.OptionT - * import cats.implicits._ - * - * val result = OptionT(List(Option(1), None, Option(3))) - * result.map(_ + 1) - * // res0: cats.data.OptionT[List, Int] = OptionT(List(Some(2), None, Some(4))) - * }}} - */ - final case class OptionT[F[_], A](value: F[Option[A]]) { def fold[B](default: => B)(f: A => B)(implicit F: Functor[F]): F[B] = From d64dcc135127966726f9b0193afc476b14251523 Mon Sep 17 00:00:00 2001 From: QueenJcloud Date: Sat, 11 Oct 2025 19:20:10 +0100 Subject: [PATCH 3/3] Updated OptionT ScalaDoc: merged example into existing comment as suggested --- core/src/main/scala/cats/data/OptionT.scala | 43 +++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/core/src/main/scala/cats/data/OptionT.scala b/core/src/main/scala/cats/data/OptionT.scala index e900783d60..f9b8fccef8 100644 --- a/core/src/main/scala/cats/data/OptionT.scala +++ b/core/src/main/scala/cats/data/OptionT.scala @@ -29,7 +29,50 @@ package data * It may also be said that `OptionT` is a monad transformer for `Option`. * * For more information, see the [[http://typelevel.org/cats/datatypes/optiont.html documentation]]. + * + * Example 1: Mapping over OptionT + * {{{ + * import cats.data.OptionT + * import cats.implicits._ + * + * val result = OptionT(List(Option(1), None, Option(3))) + * result.map(_ + 1) + * // res0: cats.data.OptionT[List, Int] = OptionT(List(Some(2), None, Some(4))) + * }}} + * + * Example 2: Using `flatMap` to chain computations + * {{{ + * val data = OptionT(List(Some(2), None)) + * val doubled = data.flatMap(x => OptionT(List(Some(x * 2)))) + * // doubled: cats.data.OptionT[List, Int] = OptionT(List(Some(4), None)) + * }}} + * + * Example 3: Using `getOrElse` to provide a default value + * {{{ + * val opt = OptionT(List(Option(5), None)) + * val result = opt.getOrElse(0) + * // result: List[Int] = List(5, 0) + * }}} + * + * Example 4: Working with asynchronous computations using `Future` + * {{{ + * import scala.concurrent.Future + * import scala.concurrent.ExecutionContext.Implicits.global + * import cats.data.OptionT + * import cats.implicits._ + * + * val fetchUser: Future[Option[String]] = Future.successful(Some("Queen")) + * val userOptionT = OptionT(fetchUser) + * + * val greeting = for { + * user <- userOptionT + * } yield s"Hello, $user!" + * + * greeting.value + * // res: scala.concurrent.Future[Option[String]] = Future(Success(Some("Hello, Queen!"))) + * }}} */ + final case class OptionT[F[_], A](value: F[Option[A]]) { def fold[B](default: => B)(f: A => B)(implicit F: Functor[F]): F[B] =