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

Removed Ops and replaced them with forwarders. #2766

Closed
wants to merge 5 commits into from
Closed
Changes from 2 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
73 changes: 8 additions & 65 deletions core/src/main/scala/cats/arrow/FunctionK.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package arrow

import scala.reflect.macros.blackbox.Context

<<<<<<< HEAD
=======
import cats.data.{EitherK, Tuple2K}

>>>>>>> 60c2fe6336f1e4dff1cd7d7eb839c19f83778b63
Copy link
Member

Choose a reason for hiding this comment

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

Merge error?

/**
* `FunctionK[F[_], G[_]]` is a functor transformation from `F` to `G`
* in the same manner that function `A => B` is a morphism from values
Expand Down Expand Up @@ -89,74 +92,14 @@ object FunctionK {
*
* Additionally, the type parameters on `f` must not be specified.
*/
def lift[F[_], G[_]](f: (F[α] ⇒ G[α]) forSome { type α }): FunctionK[F, G] =
macro FunctionKMacros.lift[F, G]
def lift[F[_]]: LiftPartiallyApplied[F] = new LiftPartiallyApplied

}

private[arrow] object FunctionKMacros {
Copy link
Contributor

Choose a reason for hiding this comment

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

We can't simply remove this code. we need to create two versions of it, one for scala 2.x with the current implementation and another with the new scala3 implementation. Like in this example
https://github.com/typelevel/cats/tree/master/core/src/main/scala-2.13%2B/cats/compat
we created two versions of cats.compat.SortedSet


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]] =
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 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)
private[arrow] final class LiftPartiallyApplied[F[_]](val dummy: Boolean = true) extends AnyVal {
type T

q"""
new _root_.cats.arrow.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")
def apply[G[_]](f: F[T] => G[T]): FunctionK[F, G] = new FunctionK[F, G] {
def apply[A](fa: F[A]): G[A] = f(fa.asInstanceOf[F[T]]).asInstanceOf[G[A]]
}

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

private[this] def punchHole(tpe: Type): Tree = tpe match {
case PolyType(undet :: Nil, underlying: TypeRef) ⇒
val α = TypeName("α")
def rebind(typeRef: TypeRef): Tree =
if (typeRef.sym == undet) tq"$α"
else {
val args = typeRef.args.map {
case ref: TypeRef => rebind(ref)
case arg => tq"$arg"
}
tq"${typeRef.sym}[..$args]"
}
val rebound = rebind(underlying)
tq"""({type λ[$α] = $rebound})#λ"""
case TypeRef(pre, sym, Nil) ⇒
tq"$sym"
case _ =>
c.abort(c.enclosingPosition, s"Unexpected type $tpe when lifting to FunctionK")
}

}

}