Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid deprecated macro APIs post 2.10 #2534

Merged
merged 4 commits into from
Sep 28, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions binCompatTest/src/main/scala/catsBC/MimaExceptions.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package catsBC
import cats.implicits._
import cats._, data._, cats.arrow._

object MimaExceptions {
import cats.implicits._

def headOption[A](list: List[A]): Option[A] = list.headOption

import cats.arrow.FunctionK // needs to be imported because of a hygiene problem

def isBinaryCompatible = (
Monad[OptionT[List, ?]],
cats.Monad[cats.data.OptionT[List, ?]],
cats.data.OptionT.catsDataTraverseForOptionT[List],
cats.data.Kleisli.catsDataCommutativeArrowForKleisliId,
cats.data.OptionT.catsDataMonoidKForOptionT[List],
Expand All @@ -15,7 +17,8 @@ object MimaExceptions {
cats.data.Kleisli.catsDataCommutativeArrowForKleisli[Option],
cats.data.Kleisli.catsDataCommutativeFlatMapForKleisli[Option, Int],
cats.data.IRWST.catsDataStrongForIRWST[List, Int, Int, Int],
cats.data.OptionT.catsDataMonadErrorMonadForOptionT[List]
cats.data.OptionT.catsDataMonadErrorMonadForOptionT[List],
FunctionK.lift(headOption)
)

}
8 changes: 7 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,12 @@ def mimaSettings(moduleName: String) = {
exclude[DirectMissingMethodProblem]("cats.data.KleisliInstances4.catsDataCommutativeFlatMapForKleisli"),
exclude[DirectMissingMethodProblem]("cats.data.IRWSTInstances1.catsDataStrongForIRWST"),
exclude[DirectMissingMethodProblem]("cats.data.OptionTInstances1.catsDataMonadErrorMonadForOptionT")
) ++ // Only compile-time abstractions (macros) allowed here
Seq(
exclude[IncompatibleMethTypeProblem]("cats.arrow.FunctionKMacros.lift"),
exclude[MissingTypesProblem]("cats.arrow.FunctionKMacros$"),
exclude[IncompatibleMethTypeProblem]("cats.arrow.FunctionKMacros#Lifter.this"),
exclude[IncompatibleResultTypeProblem]("cats.arrow.FunctionKMacros#Lifter.c")
)
}
)
Expand Down Expand Up @@ -516,10 +522,10 @@ lazy val binCompatTest = project
else //We are not testing BC on Scala 2.13 yet.
"org.typelevel" %% "cats-core" % version.value % Provided
},
"org.typelevel" %% "cats-core" % version.value % Test,
"org.scalatest" %%% "scalatest" % scalatestVersion(scalaVersion.value) % Test
)
)
.dependsOn(coreJVM % Test)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't realize that the approach in #2509 would require a publishLocal first. This here may be simpler.

Copy link
Contributor

@kailuowang kailuowang Sep 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right. I was testing against existing published jars, didn't realize that I can just do module dependency.



// cats-js is JS-only
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/scala/cats/arrow/FunctionK.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package cats
package arrow

import cats.data.{EitherK, Tuple2K}
import scala.reflect.macros.blackbox.Context

import cats.macros.MacroCompat
import cats.data.{EitherK, Tuple2K}

/**
* `FunctionK[F[_], G[_]]` is a functor transformation from `F` to `G`
Expand Down Expand Up @@ -95,7 +95,7 @@ object FunctionK {

}

private[arrow] object FunctionKMacros extends MacroCompat {
private[arrow] object FunctionKMacros {

def lift[F[_], G[_]](c: Context)(
f: c.Expr[(F[α] ⇒ G[α]) forSome { type α }]
Expand Down Expand Up @@ -124,7 +124,7 @@ private[arrow] object FunctionKMacros extends MacroCompat {
val G = punchHole(evG.tpe)

q"""
new FunctionK[$F, $G] {
new _root_.cats.arrow.FunctionK[$F, $G] {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a hygiene issue that I fixed along the way.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

def apply[A](fa: $F[A]): $G[A] = $trans(fa)
}
"""
Expand All @@ -139,7 +139,7 @@ private[arrow] object FunctionKMacros extends MacroCompat {

private[this] def punchHole(tpe: Type): Tree = tpe match {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to take a moment to appreciate the name of this method.

case PolyType(undet :: Nil, underlying: TypeRef) ⇒
val α = compatNewTypeName(c, "α")
val α = TypeName("α")
def rebind(typeRef: TypeRef): Tree =
if (typeRef.sym == undet) tq"$α"
else {
Expand Down
15 changes: 0 additions & 15 deletions macros/src/main/scala/cats/macros/compat.scala

This file was deleted.

9 changes: 9 additions & 0 deletions tests/src/test/scala/cats/tests/FunctionKSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ class FunctionKSuite extends CatsSuite {
}
}

test("hygiene") {
trait FunctionK
def optionToList[A](option: Option[A]): List[A] = option.toList
val fOptionToList = cats.arrow.FunctionK.lift(optionToList _)
forAll { (a: Option[Int]) =>
fOptionToList(a) should === (optionToList(a))
}
}

test("lift compound unary") {
val fNelFromList = FunctionK.lift[List, λ[α ⇒ Option[NonEmptyList[α]]]](NonEmptyList.fromList _)
forAll { (a: List[String]) =>
Expand Down