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

fix funK scala3 type infer #1335

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package tofu.syntax

import cats.~>

object funk {

def makeFunctionK[F[_], G[_]](maker: ContextNT[F, G]): F ~> G = toFunK(maker)

def funK[F[_], G[_]](maker: ContextNT[F, G]) = toFunK(maker)

def funKFrom[F[_]] = new Applied1[F](true)

type ContextNT[F[_], G[_]] = (arb: { type _A }) ?=> F[arb._A] => G[arb._A]

private def toFunK[F[_], G[_]](maker: ContextNT[F, G]): F ~> G =
new ~>[F, G] {
def apply[A](fa: F[A]): G[A] =
given { type _A = A } = new { type _A = A }
maker(fa)
}

final class Applied1[F[_]](private val __ : Boolean) extends AnyVal {
def apply[G[_]](maker: ContextNT[F, G]): F ~> G =
toFunK[F, G](maker)
}

// RepresentableK uses Maker
abstract class Maker[F[_], G[_], Arbitrary] extends (F ~> G) {
def applyArbitrary(f: F[Arbitrary]): G[Arbitrary]

def apply[A](fa: F[A]): G[A] = applyArbitrary(fa.asInstanceOf[F[Arbitrary]]).asInstanceOf[G[A]]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package tofu.syntax

import org.scalatest.funsuite.AnyFunSuite
import cats.{Id, ~>}

class functionKSpec extends AnyFunSuite {
def infer1(x: Id ~> Option): Unit = ()

test("funK infers type args") {
assertCompiles(
"""|import tofu.syntax.funk._
|infer1(funK(Some(_)))
|""".stripMargin
)
}

def infer2[F[_]](x: F ~> Option): Unit = ()

test("funKFrom infers type args") {
assertCompiles(
"""|import tofu.syntax.funk._
|infer2(funKFrom[Id](Some(_)))
|""".stripMargin
)
}
}
Loading