Skip to content

Commit

Permalink
Appease scalastyle
Browse files Browse the repository at this point in the history
  • Loading branch information
andyscott committed Sep 3, 2016
1 parent f9dfea0 commit 0f8578e
Showing 1 changed file with 29 additions and 26 deletions.
55 changes: 29 additions & 26 deletions core/src/main/scala/cats/arrow/FunctionK.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,41 @@ object FunctionKMacros extends MacroCompat {
f: c.Expr[(F[α] G[α]) forSome { type α }]
)(
implicit evF: c.WeakTypeTag[F[_]], evG: c.WeakTypeTag[G[_]]
): c.Expr[FunctionK[F, G]] = {
): c.Expr[FunctionK[F, G]] =
c.Expr[FunctionK[F, G]](new Lifter[c.type ](c).lift[F, G](f.tree))
// ^^note: extra space after c.type to appease scalastyle

private[this] class Lifter[C <: Context](val c: C) {
import c.universe._

def unblock(tree: Tree): Tree = tree match {
def lift[F[_], G[_]](tree: Tree)(implicit evF: c.WeakTypeTag[F[_]], evG: c.WeakTypeTag[G[_]]): Tree =
unblock(tree) match {
case q"""($param) => $trans[..$typeArgs](${ arg: Ident })""" if param.name == arg.name
typeArgs
.collect { case tt: TypeTree => tt }
.find(tt => Option(tt.original).isDefined)
.foreach { param => c.abort(param.pos,
s"type parameter $param must not be supplied when lifting function $trans to FunctionK")
}

val F = punchHole(evF.tpe)
val G = punchHole(evG.tpe)

q"""
new FunctionK[$F, $G] {
def apply[A](fa: $F[A]): $G[A] = $trans(fa)
}
"""
case other
c.abort(other.pos, s"Unexpected tree $other when lifting to FunctionK")
}

private[this] def unblock(tree: Tree): Tree = tree match {
case Block(Nil, expr) expr
case _ tree
}

def punchHole(tpe: Type): Tree = tpe match {
private[this] def punchHole(tpe: Type): Tree = tpe match {
case PolyType(undet :: Nil, underlying: TypeRef)
val α = compatNewTypeName(c, "α")
def rebind(typeRef: TypeRef): Tree =
Expand All @@ -71,29 +97,6 @@ object FunctionKMacros extends MacroCompat {
c.abort(c.enclosingPosition, s"Unexpected type $tpe when lifting to FunctionK")
}

val tree = unblock(f.tree) match {
case q"""($param) => $trans[..$typeArgs](${ arg: Ident })""" if param.name == arg.name

typeArgs
.collect { case tt: TypeTree => tt }
.find(_.original != null)
.foreach { param => c.abort(param.pos,
s"type parameter $param must not be supplied when lifting function $trans to FunctionK")
}

val F = punchHole(evF.tpe)
val G = punchHole(evG.tpe)

q"""
new FunctionK[$F, $G] {
def apply[A](fa: $F[A]): $G[A] = $trans(fa)
}
"""
case other
c.abort(other.pos, s"Unexpected tree $other when lifting to FunctionK")
}

c.Expr[FunctionK[F, G]](tree)
}

}

0 comments on commit 0f8578e

Please sign in to comment.