Assume, we have a a class with an type parameter
class A[T]
And we want to write a method, that returns objects of the type A with an arbritrary type parameter, like:
def f: A = { ... }
The compiler will complain about the missing type parameter for type A.
We can not solve this problem by writing A[Any], since e.g. A[String] is no subtype of A[Any]. But we can reach this subtype relation with a covariant annotation of +T.
Is it possible write such a method f without using covariant annotation +T ?
You can use existential types:
This is shorthand for:
You can even use upper (or lower) bounds:
You can assign any type
Tto this, however, you may not be able to do anything useful with the result