#scala #variances
For covariant type parameters we get a covariant subtype relationship regarding this type parameter. For our example this means Stack[T]
is a subtype of Stack[S]
if T
is a subtype of S
. The opposite holds for type parameters that are tagged with a -
.
If you want to change selection, open document below and click on "Move attachment"
Variances - Scala Documentationw Stack().push("hello");
s = s.push(new Object())
s = s.push(7)
println(s)
}
The annotation +T declares type T to be used only in covariant positions. Similarly, -T would declare T to be used only in contravariant positions. <span>For covariant type parameters we get a covariant subtype relationship regarding this type parameter. For our example this means Stack[T] is a subtype of Stack[S] if T is a subtype of S. The opposite holds for type parameters that are tagged with a -.
For the stack example we would have to use the covariant type parameter T in a contravariant position for being able to define method push. Since we want covariant subtyping for stacks, Summary
status | not read | | reprioritisations | |
---|
last reprioritisation on | | | suggested re-reading day | |
---|
started reading on | | | finished reading on | |
---|
Details