Manu Chadha Manu Chadha - 1 year ago 92
Scala Question

why A type is missing in function definition?

Why is the declaration of aggregate function without A in square brackets while A is being used in

seqop
?

def aggregate[B](z: B)(seqop: (B, A) ⇒ B, combop: (B, B) ⇒ B): B


instead of

def aggregate[A,B](z: B)(seqop: (B, A) ⇒ B, combop: (B, B) ⇒ B): B

Answer Source

I suppose you are talking about Scala collections. aggregate is the method of the trait GenTraversableOnce which is already parameterized by the type parameter A:

trait GenTraversableOnce[+A] {
    ...
    abstract def aggregate[B](z: ⇒ B)(seqop: (B, A) ⇒ B, combop: (B, B) ⇒ B): B
}

So, you don't need to declare another type parameter A because it is already defined! Moreover, if you declare a new type parameter A, it will shadow the existing A which will result in not what you want.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download