Sorry if this was already explained, but i didn't find similar threads anywhere in web.
Today I opened one project class in IDE and saw an error (red underline), though project was compiled successfully.
So, the code is:
public interface DatasourceImplementation<T extends Entity> {
....
}
public interface Datasource<T extends Entity> {
....
}
public interface DsContext {
@Nullable
<T extends Datasource> T get(String name);
}
And now we call this method like this:
DatasourceImplementation dsImpl = getDsContext().get("dsName");
Idea13 gives me error (incompatible types) - I think that's right.
Idea14 does not show any errors here.
JDK compiles it without errors - that's sad.
Must say, that in our project implementation class of A interface always implements B interface (possibly explains why Idea14 says it is OK), but in my opinion that can't justify this behaviour - because generally I can create class that implements A and doesn't implement B. I want static typization in my code, I do not want to see runtime class cast exceptions.
So, who's wrong here?
Upd. Add a screenshot with real classes (not sure it will explain something more, it's just the same as I described)
get()
method generic return type. – Broderget
it? – Unbeatable<T extends Datasource>
and not justDatasource
? Much more likely to correctly compile with that, and likely it's what you actually mean. Or perhaps declare<T extends Datasource>
as type parameter on your DsContext class instead of theget
method, and have that as return type instead. E.g.public <T extends Datasource> DsContext() { ... } public T get() { ... }
– Maltose