Hi,
In Scala variance can be defined with variance operators like + and - on the generic type argument. For example the List
type is covariant in the standard library.
class List[+A]
So a function with a covariant list can be defined like this:
def foo[A](list : List[A])
Also variance can be emulated with generic bounds. So we can also write this
def foo[A](list : List[_:< A])
of course this makes no sense, because list
is already covariant. But the same trick could be done for types that are not covariant. (like Stack
). Of course, also a new types could be created from stack (inheritance of aggregation) that is covariant.
So my questions:
- When should be use generic bounds for variance? And when should we create a new covariant type?
- Are generic bounds only useful for variance, or can they declare more (language concepts).
- If they are only useful for variance, are bounds then only for compatibility with Java?
thx in advance :)