reduceLeft 在某些 Seq[A] 上的签名是
The signature of reduceLeft on some Seq[A] is
def reduceLeft [B >: A] (f: (B, A) => B): BA 的类型是已知的,但是下界 >: 告诉我们 B 可以是 的任何超类型A.
The type of A is known, but the lower bound >: tells us that B can be any supertype of A.
为什么会这样?为什么不
Why is it like this? Why not
def reduceLeft (f: (A, A) => A): A我们已经知道序列的头部是 A 类型,所以我想不出 B 怎么可能不等于 A.你能提供一个例子,其中 B 是某种超类型吗?
We already know that the head of the sequence is type A and so I can't think of how B could be anything other than equal to A. Can you provide an example where B is some super-type?
推荐答案假设你的类 B 有一个方法 combine(other:B): B.现在您在 A 列表上调用 reduceLeft((b,a) => bbine(a)).由于combine的返回类型是B,reduceLeft的类型参数需要是B.
Let's say your class B has a method combine(other:B): B. Now you call reduceLeft((b,a) => bbine(a)) on a list of As. Since the return type of combine is B the type parameter to reduceLeft needs to be B.
更多推荐
为什么reduceLeft 的类型参数包含下界?
发布评论