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
Show file tree
Hide file tree
Changes from all 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
70 changes: 5 additions & 65 deletions core/src/main/scala/cats/arrow/FunctionK.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,74 +89,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")
}

}

}
9 changes: 5 additions & 4 deletions core/src/main/scala/cats/syntax/eq.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cats
package syntax

import cats.macros.Ops

trait EqSyntax {

/** not final so it can be disabled in favor of scalactic equality in tests */
Expand All @@ -11,8 +9,11 @@ trait EqSyntax {
}

final class EqOps[A: Eq](lhs: A) {
def ===(rhs: A): Boolean = macro Ops.binop[A, Boolean]
def =!=(rhs: A): Boolean = macro Ops.binop[A, Boolean]

def ===(rhs: A): Boolean = implicitly[Eq[A]].eqv(lhs, rhs)
def =!=(rhs: A): Boolean = implicitly[Eq[A]].neqv(lhs, rhs)

def eqv(rhs: A): Boolean = Eq[A].eqv(lhs, rhs)
def neqv(rhs: A): Boolean = Eq[A].neqv(lhs, rhs)

}
8 changes: 3 additions & 5 deletions core/src/main/scala/cats/syntax/group.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package cats
package syntax

import cats.macros.Ops

trait GroupSyntax extends SemigroupSyntax {
// TODO: use simulacrum instances eventually
implicit final def catsSyntaxGroup[A: Group](a: A): GroupOps[A] =
new GroupOps[A](a)
}

final class GroupOps[A: Group](lhs: A) {
def |-|(rhs: A): A = macro Ops.binop[A, A]
def remove(rhs: A): A = macro Ops.binop[A, A]
def inverse(): A = macro Ops.unop[A]
def |-|(rhs: A): A = implicitly[Group[A]].remove(lhs, rhs)
def remove(rhs: A): A = implicitly[Group[A]].remove(lhs, rhs)
def inverse(): A = implicitly[Group[A]].inverse(lhs)
}
9 changes: 4 additions & 5 deletions core/src/main/scala/cats/syntax/order.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cats
package syntax

import cats.macros.Ops
import cats.kernel.Comparison

trait OrderSyntax extends PartialOrderSyntax {
Expand All @@ -10,8 +9,8 @@ trait OrderSyntax extends PartialOrderSyntax {
}

final class OrderOps[A: Order](lhs: A) {
def compare(rhs: A): Int = macro Ops.binop[A, Int]
def min(rhs: A): A = macro Ops.binop[A, A]
def max(rhs: A): A = macro Ops.binop[A, A]
def comparison(rhs: A): Comparison = macro Ops.binop[A, Comparison]
def compare(rhs: A): Int = implicitly[Order[A]].compare(lhs, rhs)
def min(rhs: A): A = implicitly[Order[A]].min(lhs, rhs)
def max(rhs: A): A = implicitly[Order[A]].max(lhs, rhs)
def comparison(rhs: A): Comparison = implicitly[Order[A]].comparison(lhs, rhs)
}
18 changes: 8 additions & 10 deletions core/src/main/scala/cats/syntax/partialOrder.scala
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package cats
package syntax

import cats.macros.Ops

trait PartialOrderSyntax extends EqSyntax {
implicit final def catsSyntaxPartialOrder[A: PartialOrder](a: A): PartialOrderOps[A] =
new PartialOrderOps[A](a)
}

final class PartialOrderOps[A](lhs: A)(implicit A: PartialOrder[A]) {
def >(rhs: A): Boolean = macro Ops.binop[A, Boolean]
def >=(rhs: A): Boolean = macro Ops.binop[A, Boolean]
def <(rhs: A): Boolean = macro Ops.binop[A, Boolean]
def <=(rhs: A): Boolean = macro Ops.binop[A, Boolean]
def >(rhs: A): Boolean = A.gt(lhs, rhs)
def >=(rhs: A): Boolean = A.gteqv(lhs, rhs)
def <(rhs: A): Boolean = A.lt(lhs, rhs)
def <=(rhs: A): Boolean = A.lteqv(lhs, rhs)

def partialCompare(rhs: A): Double = macro Ops.binop[A, Double]
def tryCompare(rhs: A): Option[Int] = macro Ops.binop[A, Option[Int]]
def pmin(rhs: A): Option[A] = macro Ops.binop[A, Option[A]]
def pmax(rhs: A): Option[A] = macro Ops.binop[A, Option[A]]
def partialCompare(rhs: A): Double = A.partialCompare(lhs, rhs)
def tryCompare(rhs: A): Option[Int] = A.tryCompare(lhs, rhs)
def pmin(rhs: A): Option[A] = A.pmin(lhs, rhs)
def pmax(rhs: A): Option[A] = A.pmax(lhs, rhs)
}
8 changes: 3 additions & 5 deletions core/src/main/scala/cats/syntax/semigroup.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package cats
package syntax

import cats.macros.Ops

trait SemigroupSyntax {
// TODO: use simulacrum instances eventually
implicit final def catsSyntaxSemigroup[A: Semigroup](a: A): SemigroupOps[A] =
new SemigroupOps[A](a)
}

final class SemigroupOps[A: Semigroup](lhs: A) {
def |+|(rhs: A): A = macro Ops.binop[A, A]
def combine(rhs: A): A = macro Ops.binop[A, A]
def combineN(rhs: Int): A = macro Ops.binop[A, A]
def |+|(rhs: A): A = implicitly[Semigroup[A]].combine(lhs, rhs)
def combine(rhs: A): A = implicitly[Semigroup[A]].combine(lhs, rhs)
def combineN(rhs: Int): A = implicitly[Semigroup[A]].combineN(lhs, rhs)
}