3 ms·
> I disagree. I don't know what you're disagreeing with. > And then it becomes clear that the foos in bar will never be a Foo, because Foo does not implement
by munificent 10y ago
> I disagree.
I don't know what you're disagreeing with.
> And then it becomes clear that the foos in bar will never be a Foo, because Foo does not implement IFoo<String> and never will.
The statement "Foo does not implement IFoo<String>" is not meaningful. "Foo" is not a type, it's a type constructor. To refer to a type, you need to say Foo of what. And it is the case that Foo<String> implements IFoo<String>.
Consider:
interface IFoo<TUnconstrained> {
Number foo(TUnconstrained t);
}
void useFoo(IFoo<String> foo) {
Number n = foo.foo("not a number");
}
This code compiles fine. Add this:
class Foo<TExtendsNumber extends Number> implements IFoo<TExtendsNumber> {
public Number foo(TExtendsNumber t) { return t; }
}
It still compiles fine. This is exactly what the author is saying. The reason it is fine for the compiler to permit the above code is because it is relying on the fact that you will not be able to instantiate Foo<String>.
This is one half of the puzzle of the actual unsoundness example. The other half is that you can then sidestep that restriction using a wildcard to define a type that hypothetically meets the required bound even though no such real type exists.
That would still be safe, since you've never be able to initialize that variable since no value of that type is possible. Except that Java lets you initialize it with null.