Running test covariant in T means that Test[A] is a subtype of Test[Any] for any A So let's create a Test :
val test_string = new Test[String]
Now we have Test[String] , and the contained list is of type List[String] .
Since Test[String] is a subtype of Test[Any] , it should be allowed:
val test_any : Test[Any] = test_string
And now we have Test[Any] , so test_any.list is a type of List[Any] , which means the following should be true:
test_any.list = List[Any]()
This means that we just assigned a List[Any] list item to test_strings, which should not be allowed, as it is assumed to be List[String] , not List[Any] . It also means that you could add anything at all to the list, since it is a type of List[Any] .
stew Jan 29 '13 at 14:23 2013-01-29 14:23
source share