0

由于某种原因,以下无法正常工作

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

test当我在自然变换上调用该方法时。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 类型或单个参数更高种类的类型同样失败)

4

1 回答 1

0

可能与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
  }
}

如果目标是扩展NtTestNatTransExt1如果一个人想尽可能保持通用,这可能是最好的选择。NatTransExt2没关系,如果扩展真的特定于NtTest.

于 2017-06-08T23:56:34.507 回答