Bhashit Parikh
Bhashit Parikh

Reputation: 3131

Scala covariant type declaration with an upper bound, why do methods need to repeat the upper bound exlplicitly

I have a trait that looks like this:

trait Processor[+T <: Document] {
  def process[D >: T <: Document](doc: D)
}

If I declare the process method with process[D >: T](doc: D), I can't access the methods from the Document class.

I don't know why do I need to repeat the upper bound, the <: Document, in the process method.

So, two questions:

  1. Is this the way it's supposed to be used?
  2. Why doesn't the type system automatically pick up the upper bound from the trait definition.

Upvotes: 1

Views: 120

Answers (1)

Cyrille Corpet
Cyrille Corpet

Reputation: 5315

The upper bound in your method is on D, not on T. Say you do not put that upper bound, then D could be anything that T also is, for instance, Any. So the compiler must assume that D could be Any, and therefore cannot give you more methods.

Upvotes: 2

Related Questions