2

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?

Luigi Plinge
  • 50,650
  • 20
  • 113
  • 180

1 Answers1

3

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 As. Since the return type of combine is B the type parameter to reduceLeft needs to be B.

sepp2k
  • 363,768
  • 54
  • 674
  • 675