From db5b07f1ea8b6b6876380b20880b8e574e2cb194 Mon Sep 17 00:00:00 2001 From: yilinwei Date: Tue, 16 May 2017 23:52:58 +0100 Subject: [PATCH] changing canResolve to implicitlyH and simplifying macro --- ...lve.scala => implicitResolutionSpec.scala} | 18 +++++++-------- .../cats/tests/ImplicitResolutionTests.scala | 23 ++++++++++--------- 2 files changed, 20 insertions(+), 21 deletions(-) rename macros/src/main/scala/cats/macros/{canResolve.scala => implicitResolutionSpec.scala} (71%) diff --git a/macros/src/main/scala/cats/macros/canResolve.scala b/macros/src/main/scala/cats/macros/implicitResolutionSpec.scala similarity index 71% rename from macros/src/main/scala/cats/macros/canResolve.scala rename to macros/src/main/scala/cats/macros/implicitResolutionSpec.scala index e6a627d0c3..9e50523cf7 100644 --- a/macros/src/main/scala/cats/macros/canResolve.scala +++ b/macros/src/main/scala/cats/macros/implicitResolutionSpec.scala @@ -6,11 +6,11 @@ import reflect.macros.blackbox import macrocompat.bundle trait ImplicitResolutionSpec { - def canResolve[A, F[_]](imports: Unit): F[A] = macro CanResolveMacro.mkImplicitlyBlock[A, F] - def canResolveK[G[_], F[_[_]]](imports: Unit): F[G] = macro CanResolveMacroK.mkImplicitlyBlock[G, F] + def implicitlyH[A, F[_]]: F[A] = macro ImplicitlyHMacro.mkImplicitlyBlock[A, F] + def implicitlyHK[G[_], F[_[_]]]: F[G] = macro ImplicitlyHKMacro.mkImplicitlyBlock[G, F] } -object CanResolveMacro { +object ImplicitlyHMacros { val filterClasses = Set( "scala.Serializable", "java.io.Serializable", @@ -19,19 +19,18 @@ object CanResolveMacro { ) } -import CanResolveMacro._ +import ImplicitlyHMacros._ @bundle -final class CanResolveMacro(val c: blackbox.Context) { +final class ImplicitlyHMacro(val c: blackbox.Context) { import c.universe._ - def mkImplicitlyBlock[A, F[_]](imports: c.Expr[Unit])( + def mkImplicitlyBlock[A, F[_]]( implicit wtf: c.WeakTypeTag[F[_]], wta: c.WeakTypeTag[A]): c.Expr[F[A]] = { val resolve = wtf.tpe.baseClasses.filterNot(cls => filterClasses.contains(cls.fullName)).map(sym => q"implicitly[$sym[$wta]]") val expr = c.Expr[F[A]](q""" { - ..$imports ..${resolve.reverse} } """) @@ -40,11 +39,11 @@ final class CanResolveMacro(val c: blackbox.Context) { } @bundle -final class CanResolveMacroK(val c: blackbox.Context) { +final class ImplicitlyHKMacro(val c: blackbox.Context) { import c.universe._ - def mkImplicitlyBlock[G[_], F[_[_]]](imports: c.Expr[Unit])( + def mkImplicitlyBlock[G[_], F[_[_]]]( implicit wtf: c.WeakTypeTag[F[G]], wta: c.WeakTypeTag[G[_]]): c.Expr[F[G]] = { val curried: List[Type] = wtf.tpe match { case p: PolyType => @@ -66,7 +65,6 @@ final class CanResolveMacroK(val c: blackbox.Context) { } c.Expr[F[G]](q""" { - ..$imports ..${resolve.reverse} } """) diff --git a/tests/src/test/scala/cats/tests/ImplicitResolutionTests.scala b/tests/src/test/scala/cats/tests/ImplicitResolutionTests.scala index 5da1541c21..5cfd3ec0e0 100644 --- a/tests/src/test/scala/cats/tests/ImplicitResolutionTests.scala +++ b/tests/src/test/scala/cats/tests/ImplicitResolutionTests.scala @@ -8,25 +8,26 @@ import cats.data._ final class ImplicitResolutionTests extends ImplicitResolutionSpec { - canResolve[String, Monoid] { - import cats.implicits._ - } + { + import cats.implicits._ - canResolveK[Order, Contravariant] { - import cats.implicits._ + implicitlyH[String, Monoid] + implicitlyHK[Order, Contravariant] + implicitlyHK[Option, MonadError[?[_], Unit]] } - canResolveK[Order, Contravariant] { - import cats.instances.order._ + { + import cats.instances.order._ + implicitlyHK[Order, Contravariant] } - canResolveK[Option, MonadError[?[_], Unit]] { - import cats.implicits._ - } + { - canResolveK[WriterT[ListWrapper, ListWrapper[Int], ?], Monad] { import cats.implicits._ + implicit val F: MonadCombine[ListWrapper] = ListWrapper.monadCombine implicit val L: Monoid[ListWrapper[Int]] = ListWrapper.monoid[Int] + + implicitlyHK[WriterT[List, List[Int], ?], Monad] } }