From 66d9e31995ce5e5f5f0ac6a064baf1d288f45095 Mon Sep 17 00:00:00 2001 From: Daniel Karch Date: Sun, 20 May 2018 22:06:12 +0200 Subject: [PATCH 1/4] Add `.nested` syntax. --- core/src/main/scala/cats/syntax/all.scala | 1 + core/src/main/scala/cats/syntax/nested.scala | 27 +++++++++++++++++++ core/src/main/scala/cats/syntax/package.scala | 1 + 3 files changed, 29 insertions(+) create mode 100644 core/src/main/scala/cats/syntax/nested.scala diff --git a/core/src/main/scala/cats/syntax/all.scala b/core/src/main/scala/cats/syntax/all.scala index dec09ed8e0..e25d923603 100644 --- a/core/src/main/scala/cats/syntax/all.scala +++ b/core/src/main/scala/cats/syntax/all.scala @@ -38,6 +38,7 @@ trait AllSyntax with MonadErrorSyntax with MonadSyntax with MonoidSyntax + with NestedSyntax with OptionSyntax with OrderSyntax with ParallelSyntax diff --git a/core/src/main/scala/cats/syntax/nested.scala b/core/src/main/scala/cats/syntax/nested.scala new file mode 100644 index 0000000000..c31af329d2 --- /dev/null +++ b/core/src/main/scala/cats/syntax/nested.scala @@ -0,0 +1,27 @@ +package cats +package syntax + +import cats.data.Nested + +trait NestedSyntax { + implicit final def catsSyntaxNestedId[F[_], G[_], A](value: F[G[A]]): NestedIdOps[F, G, A] = + new NestedIdOps[F, G, A](value) +} + +final class NestedIdOps[F[_], G[_], A](val value: F[G[A]]) extends AnyVal { + /** + * Wrap a value in `Nested`. + * + * `x.nested` is equivalent to `Nested(x)`. + * + * Example: + * {{{ + * scala> import cats.implicits._ + * scala> List(Some(3), None).nested.map(_+1).value + * res0: List[Option[Int]] = List(Some(4), None) + * }}} + */ + def nested: Nested[F, G, A] = Nested[F, G, A](value) +} + + diff --git a/core/src/main/scala/cats/syntax/package.scala b/core/src/main/scala/cats/syntax/package.scala index b928818bc9..e83d5cdcf9 100644 --- a/core/src/main/scala/cats/syntax/package.scala +++ b/core/src/main/scala/cats/syntax/package.scala @@ -33,6 +33,7 @@ package object syntax { object monad extends MonadSyntax object monadError extends MonadErrorSyntax object monoid extends MonoidSyntax + object nested extends NestedSyntax object option extends OptionSyntax object order extends OrderSyntax object parallel extends ParallelSyntax From 66aaf13fbf6d16933ee524957fbec2a34fd95c00 Mon Sep 17 00:00:00 2001 From: Daniel Karch Date: Mon, 21 May 2018 11:04:27 +0200 Subject: [PATCH 2/4] Add test for new `.nested` syntax. --- tests/src/test/scala/cats/tests/SyntaxSuite.scala | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/src/test/scala/cats/tests/SyntaxSuite.scala b/tests/src/test/scala/cats/tests/SyntaxSuite.scala index 6bcd3b987b..70c2113117 100644 --- a/tests/src/test/scala/cats/tests/SyntaxSuite.scala +++ b/tests/src/test/scala/cats/tests/SyntaxSuite.scala @@ -2,6 +2,7 @@ package cats package tests import cats.arrow.Compose +import cats.data.Nested import cats.instances.AllInstances import cats.syntax.AllSyntax @@ -338,5 +339,11 @@ object SyntaxSuite extends AllInstances with AllSyntax { val gea4 = ga.recoverWith(pfegea) } + def testNested[F[_], G[_], A]: Unit = { + val fga: F[G[A]] = mock[F[G[A]]] + + val nested: Nested[F, G, A] = fga.nested + } + } From eac09da1f3526a9c9e35aea36229701e59dd6119 Mon Sep 17 00:00:00 2001 From: Daniel Karch Date: Mon, 21 May 2018 20:10:13 +0200 Subject: [PATCH 3/4] Fix binary compatibility issue. --- core/src/main/scala/cats/syntax/all.scala | 5 +++-- tests/src/test/scala/cats/tests/SyntaxSuite.scala | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/core/src/main/scala/cats/syntax/all.scala b/core/src/main/scala/cats/syntax/all.scala index e25d923603..87c2497f04 100644 --- a/core/src/main/scala/cats/syntax/all.scala +++ b/core/src/main/scala/cats/syntax/all.scala @@ -38,7 +38,6 @@ trait AllSyntax with MonadErrorSyntax with MonadSyntax with MonoidSyntax - with NestedSyntax with OptionSyntax with OrderSyntax with ParallelSyntax @@ -60,4 +59,6 @@ trait AllSyntaxBinCompat0 with ApplicativeErrorExtension with TrySyntax -trait AllSyntaxBinCompat1 extends FlatMapOptionSyntax +trait AllSyntaxBinCompat1 + extends FlatMapOptionSyntax + with NestedSyntax diff --git a/tests/src/test/scala/cats/tests/SyntaxSuite.scala b/tests/src/test/scala/cats/tests/SyntaxSuite.scala index 70c2113117..b57a55f732 100644 --- a/tests/src/test/scala/cats/tests/SyntaxSuite.scala +++ b/tests/src/test/scala/cats/tests/SyntaxSuite.scala @@ -4,7 +4,7 @@ package tests import cats.arrow.Compose import cats.data.Nested import cats.instances.AllInstances -import cats.syntax.AllSyntax +import cats.syntax.{AllSyntax, NestedSyntax} /** @@ -25,7 +25,7 @@ import cats.syntax.AllSyntax * * None of these tests should ever run, or do any runtime checks. */ -object SyntaxSuite extends AllInstances with AllSyntax { +object SyntaxSuite extends AllInstances with AllSyntax with NestedSyntax { // pretend we have a value of type A def mock[A]: A = ??? From 150da99f5fb0b362e5e6f94e9f465a3c03b2d832 Mon Sep 17 00:00:00 2001 From: Daniel Karch Date: Mon, 21 May 2018 20:47:09 +0200 Subject: [PATCH 4/4] Change mixin in SyntaxSuite. --- tests/src/test/scala/cats/tests/SyntaxSuite.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/src/test/scala/cats/tests/SyntaxSuite.scala b/tests/src/test/scala/cats/tests/SyntaxSuite.scala index b57a55f732..5dc48aa6c0 100644 --- a/tests/src/test/scala/cats/tests/SyntaxSuite.scala +++ b/tests/src/test/scala/cats/tests/SyntaxSuite.scala @@ -4,7 +4,7 @@ package tests import cats.arrow.Compose import cats.data.Nested import cats.instances.AllInstances -import cats.syntax.{AllSyntax, NestedSyntax} +import cats.syntax.{AllSyntax, AllSyntaxBinCompat1} /** @@ -25,7 +25,7 @@ import cats.syntax.{AllSyntax, NestedSyntax} * * None of these tests should ever run, or do any runtime checks. */ -object SyntaxSuite extends AllInstances with AllSyntax with NestedSyntax { +object SyntaxSuite extends AllInstances with AllSyntax with AllSyntaxBinCompat1 { // pretend we have a value of type A def mock[A]: A = ???