Skip to content

Commit

Permalink
Appease Scala 2.10 macro gods
Browse files Browse the repository at this point in the history
  • Loading branch information
andyscott committed Sep 3, 2016
1 parent 6bdc2ae commit f9dfea0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
19 changes: 9 additions & 10 deletions core/src/main/scala/cats/arrow/FunctionK.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package arrow

import cats.data.Coproduct

import reflect.macros.blackbox.Context
import cats.macros.MacroCompat

trait FunctionK[F[_], G[_]] extends Serializable { self =>
def apply[A](fa: F[A]): G[A]
Expand Down Expand Up @@ -37,13 +37,12 @@ object FunctionK {

}

object FunctionKMacros {
object FunctionKMacros extends MacroCompat {

def lift[
F[_]: λ[α[_] c.WeakTypeTag[α[_]]],
G[_]: λ[α[_] c.WeakTypeTag[α[_]]]
](c: Context)(
f: c.Expr[F[α] G[α]] forSome { type α }
def lift[F[_], G[_]](c: Context)(
f: c.Expr[(F[α] G[α]) forSome { type α }]
)(
implicit evF: c.WeakTypeTag[F[_]], evG: c.WeakTypeTag[G[_]]
): c.Expr[FunctionK[F, G]] = {
import c.universe._

Expand All @@ -54,7 +53,7 @@ object FunctionKMacros {

def punchHole(tpe: Type): Tree = tpe match {
case PolyType(undet :: Nil, underlying: TypeRef)
val α = TypeName("α")
val α = compatNewTypeName(c, "α")
def rebind(typeRef: TypeRef): Tree =
if (typeRef.sym == undet) tq""
else {
Expand Down Expand Up @@ -82,8 +81,8 @@ object FunctionKMacros {
s"type parameter $param must not be supplied when lifting function $trans to FunctionK")
}

val F = punchHole(weakTypeTag[F[_]].tpe)
val G = punchHole(weakTypeTag[G[_]].tpe)
val F = punchHole(evF.tpe)
val G = punchHole(evG.tpe)

q"""
new FunctionK[$F, $G] {
Expand Down
15 changes: 15 additions & 0 deletions macros/src/main/scala/cats/macros/compat.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cats
package macros

/** Macro compatibility.
*
* Used only to push deprecation errors in core off into
* the macros project, as warnings.
*/
private[cats] class MacroCompat {

type Context = reflect.macros.Context
def compatNewTypeName(c: Context, name: String): c.TypeName =
c.universe.newTypeName(name)

}
8 changes: 4 additions & 4 deletions tests/src/test/scala/cats/tests/FunctionKTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,24 @@ class FunctionKTests extends CatsSuite {

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

val fO2I: FunctionK[Option, Iterable] = FunctionK.lift(Option.option2Iterable)
val fO2I: FunctionK[Option, Iterable] = FunctionK.lift(Option.option2Iterable _)
forAll { (a: Option[String]) =>
fO2I(a).toList should === (Option.option2Iterable(a).toList)
}

val fNelFromListUnsafe = FunctionK.lift(NonEmptyList.fromListUnsafe)
val fNelFromListUnsafe = FunctionK.lift(NonEmptyList.fromListUnsafe _)
forAll { (a: NonEmptyList[Int]) =>
fNelFromListUnsafe(a.toList) should === (NonEmptyList.fromListUnsafe(a.toList))
}
}

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

0 comments on commit f9dfea0

Please sign in to comment.