我有一个Foo
有两个参数的类,我正在尝试为 Foo 编写一个 Functor 实例,其中第一个参数是固定的,如下所示:
object Scratchpad {
trait Functor[F[_]] {
def fmap[A, B](f: A => B): F[A] => F[B]
}
case class Foo[X, Y](value: Y)
implicit def fooInstances[X]: Functor[Foo[X, _]] =
new Functor[Foo[X, _]] {
def fmap[A, B](f: A => B): Foo[X, A] => Foo[X, B] =
foo => Foo[X, B](f(foo.value))
}
}
但是上面的代码编译失败,产生如下错误:
Error:(9, 41) Scratchpad.Foo[X, _] takes no type parameters, expected: one
implicit def fooInstances[X]: Functor[Foo[X, _]] =
我知道 Scalaz 对他们的\/
类型做了类似的事情,但是检查他们的源代码会发现一个奇怪的?
,它不能为我编译:
implicit def DisjunctionInstances1[L]: Traverse[L \/ ?] with Monad[L \/ ?] with BindRec[L \/ ?] with Cozip[L \/ ?] with Plus[L \/ ?] with Optional[L \/ ?] with MonadError[L \/ ?, L] =
Scalaz 是如何?
工作的,我该如何编写 Functor 实例Foo
?