Scalaz `F[_] : Applicative` 类型约束如何暗示使用隐式参数?
我正在努力理解 Scalaz 中的遍历
特征:
def traverse[F[_] : Applicative, A, B](f: A => F[B], t: T[A]): F[T[B]]
我不明白的部分是 F[_] : Applicative
。
现在,让我们看看 Applicative
是:
trait Applicative[Z[_]] extends Pointed[Z] with Apply[Z] {
override def fmap[A, B](fa: Z[A], f: A => B): Z[B] = this(pure(f), fa)
override def apply[A, B](f: Z[A => B], a: Z[A]): Z[B] = liftA2(f, a, (_:A => B)(_: A))
def liftA2[A, B, C](a: Z[A], b: Z[B], f: (A, B) => C): Z[C] = apply(fmap(a, f.curried), b)
}
在这里,为了使 traverse
适用于某种类型 F
,需要引入一个 Applicative[F 类型的隐式对象]
在范围内。
我想了解几件事:
F[_] : Applicative
到底是什么意思?- 为什么
F[_]
与Applicative
有关?我们需要Applicative[F],而不是F[something]扩展Applicative,对吧? - 为什么此方法使用
Applicative[F]
类型的隐式值而不声明隐式参数?
I am struggling to understand the following function definition in Traverse
trait in Scalaz:
def traverse[F[_] : Applicative, A, B](f: A => F[B], t: T[A]): F[T[B]]
The part I don't understand is F[_] : Applicative
.
Now, let's see what Applicative
is:
trait Applicative[Z[_]] extends Pointed[Z] with Apply[Z] {
override def fmap[A, B](fa: Z[A], f: A => B): Z[B] = this(pure(f), fa)
override def apply[A, B](f: Z[A => B], a: Z[A]): Z[B] = liftA2(f, a, (_:A => B)(_: A))
def liftA2[A, B, C](a: Z[A], b: Z[B], f: (A, B) => C): Z[C] = apply(fmap(a, f.curried), b)
}
Here, for traverse
to work for some type F
, one needs to bring an implicit object of type Applicative[F]
in scope.
I'd like to understand several things:
- Wat exactly does
F[_] : Applicative
mean? - Why does
F[_]
has something to do withApplicative
? We needApplicative[F]
, not F[something] extends Applicative right? - Why does this method use implicit values of type
Applicative[F]
without declaring implicit parameters?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为所有三个问题都可以用以下表示法来回答:
相当于:
第一个表示法称为 F[_] 的上下文绑定。
I think all three questions can be answered with the fact that this notation:
is equivalent to this:
The first notation is known as a context bound for
F[_]
.