Closed jvasileff closed 9 years ago
sup.newList<String>()
passes String
to the X
type parameter, but there's no such thing in the refined implementation (it's Y
, that's why the error goes away if the type parameter has the same name in the refined method).
A "quick" fix would be checking if the type parameter is refined, and if it has a different name from the super declaration, then check for both names in the type arguments. It will bloat the code, but the alternative is to look for the super declarations at runtime...
I found a nicer solution, I just have to test it against a deeper hierarchy
BTW variance had nothing to do with this, it was about missing type arguments. So the second, shorter, example is the one I'll use as official test.
This program:
outputs:
but should output four trues. Values produced by
sub
andsup
should have the same runtime types sincesub === sup
.A few items to note:
SuperFactory
usesX
for its methods type parameter, andSubFactory
usesY
. If these identifiers are renamed to be identical, the problem goes away.SuperContainer
is made covariant inElement
, the output becomestrue true false true
SubContainer
is also made covariant inElement
, the output becomestrue true true true