admin管理员组

文章数量:1357307

Consider this example from book Programming in Scala, 5th Edition by Martin Odersky (Chapter 18 · Type Parameterization)

Here - the following class Cat is described:

abstract class Cat[-T, +U]:
def meow[W−](volume: T−, listener: Cat[U+, T−]−)
: Cat[Cat[U+, T−]−, U+]+
  • the concepts of covariance and contravariance for the class level type parameters T and U perfectly make sense.

But the fact that the method level type variable W is being classified as -ve ie as Contravariant - is not very clear. Since we are neither using it in the method argument determination - nor are we using it in the return type.

Why is the example quoting it as contravariant and not invariant?

本文标签: genericsVariance Annotation for an independent Method level type parameters in scalaStack Overflow