Skip to content

Commit

Permalink
changing canResolve to implicitlyH and simplifying macro
Browse files Browse the repository at this point in the history
  • Loading branch information
yilinwei committed May 16, 2017
1 parent c0d8f24 commit db5b07f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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}
}
""")
Expand All @@ -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 =>
Expand All @@ -66,7 +65,6 @@ final class CanResolveMacroK(val c: blackbox.Context) {
}
c.Expr[F[G]](q"""
{
..$imports
..${resolve.reverse}
}
""")
Expand Down
23 changes: 12 additions & 11 deletions tests/src/test/scala/cats/tests/ImplicitResolutionTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
}

0 comments on commit db5b07f

Please sign in to comment.