I am trying to understand how the compiler checks whether the position for a type parameter is covariant or contravariant.
As far as I know, if the type parameter is annotated with the +, which is the covariant annotation, then any method cannot have a input parameter typed with that class/trait's type parameter.
For example, bar cannot have a parameter of type T.
class Foo[+T] {
def bar(param: T): Unit =
println("Hello foo bar")
}
Because the position for the parameter of bar() is considered to be negative, which means any type parameter in that position is in a contravariant position.
I am curious how the Scala compiler can find if every location in the class/trait is positive, negative, or neutral. It seems that there exist some rules like flipping its position in some condition but couldn't understand it clearly.
Also, if possible, I would like to know how these rules are defined. For example, it seems that parameters for methods defined in a class that has covariant annotation, like bar() method in Foo class, should have contravariant class type. Why?