Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top