Reputation: 51109
The signature of reduceLeft
on some Seq[A]
is
def reduceLeft [B >: A] (f: (B, A) => B): B
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
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?
Upvotes: 2
Views: 180
Reputation: 370172
Let's say your class B has a method combine(other:B): B
. Now you call reduceLeft((b,a) => b.combine(a))
on a list of A
s. Since the return type of combine
is B
the type parameter to reduceLeft
needs to be B
.
Upvotes: 3