Scala无法在scalaz或cats中执行NaturalTransformation



由于某些原因,以下无法工作

object NtExtTest {
  implicit class NaturalTransformExt[M[_], N[_]](val self: NaturalTransformation[M,N]) extends AnyVal {
    def test(b:Boolean) = b
  }
 }

当我对自然变换调用方法CCD_ 1时。Intellij将其识别为一个扩展函数,但编译给出了value test is not a member of cats.~>。使用scalaz NaturalTransformation时也会发生同样的情况。我能做些什么来帮助编译识别扩展吗?

Scala版本为2.11.8

一个失败的例子:

  import NtExtTest._
  class NtTest[B] extends NaturalTransformation[Either[B,?], Xor[B,?]] {
    def apply[A](fa: Either[B, A]): Xor[B, A] = {
      fa match {
        case Left(l) => Xor.left(l)
        case Right(r) => Xor.right(r)
      }
    }
  }
  val test = new NtTest[String]
  test.test(false)

(上面使用了种类投影插件,但同样适用于类型lambdas或单参数更高的kindd类型)

可能与SI-8286 有关

object NtExtTest {
  // this will work
  implicit class NatTransExt1[E](val t: NaturalTransformation[Either[E, ?], /[E, ?]]) {
    def test1(b: Boolean): Boolean = false
  }
  // and this will work
  implicit class NatTransExt2[E](val t: NtTest[E]) {
    def test2(b: Boolean): Boolean = false
  }
  // but this will not work, because NaturalTransformation[F, G] and
  // NaturalTransformation[Either[E, ?], /[E, ?]] of different kind
  implicit class NatTransExt3[F[_], G[_]](val s: NaturalTransformation[F, G]) {
    def test3(b: Boolean): Boolean = false
  }
}

即与NaturalTransformation无关。它在简单的情况下也不起作用:

trait SomeType[F[_]]
class SomeConcreteType[A] extends SomeType[Either[A, ?]]
object SomeTypeExt {
  // this will not compile
  implicit class SomeTypeEnrich[F[_]](val t: SomeType[F]) {
    def test1: Boolean = false
  }
  // this will
  implicit class SomeConcreteEnrich[A](val t: SomeType[Either[A, ?]]) {
    def test2: Boolean = true
  }
}

如果目标是扩展NtTest,那么如果希望尽可能保持通用性,那么NatTransExt1可能是最佳选择。如果扩展确实特定于NtTest,那么NatTransExt2是可以的。

最新更新