New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inventory API query return Type #1741
Comments
The general idea of the pseudo-duck-typing afforded by the bounded generic was that people are likely going to query and immediately cast to the desired type, the generic just elides the explicit cast. Neither is ideal but I felt when I designed it that the duck typing was just cleaner in situations that the query is known to succeed.
From an "encouraging people to do the right thing" point of view, I think the suggestion makes sense. From a totally misanthropic perspective I don't think it will actually do that. |
This is something that I've wanted ever since our conversation a couple weeks ago. The fact that this behavior isn't well documented is made worse by the improper use of generics. It's a pitfall for many people who are just starting to work with Sponge or the InventoryAPI. My recommendation is to have an inventory containing the result that respects generics, much like the The result should also not comtain an empty inventory. Instead, methods like |
@Mumfrey @SimonFlash |
The result is already being wrapped - the inventory you receive isn't an inventory per se, it's a view of the inventories returned by the query. The class returned should make it clear that A: it is not the same thing that was queried for, and B: it contains an unknown (non-negative) number of inventories that is what was queried for. |
Not always. Currently if you get a single inventory matching the query you get the inventory itself. |
How about making public interface Inventory {
<E extends Inventory> ResultInventoryAdapter<E> query(Class<E> type);
// other methods here
}
public interface GridInventory extends Inventory {}
// Because this is final, the compiler already knows the full hierarchy (excluding any mixin antics).
// So it can generate errors like the one below.
public final class ResultInventoryAdapter<E extends Inventory> implements Inventory {
public <E2 extends Inventory> ResultInventoryAdapter<E2> query(Class<E2> type) {
return new ResultInventoryAdapter<>();
}
// other methods here
}
public void test(Inventory anyInventoryObject) {
// Compiler errors here: "Inconvertible types; cannot cast ResultInventoryAdapter to GridInventory"
GridInventory inv = (GridInventory) anyInventoryObject.query(GridInventory.class);
} |
see #1848 |
I see this kind of code very often:
Which is fine if you are sure you will get the specific type of inventory back.
But can also result in ClassCastException because the result can also be:
One idea to reduce confusion about this would be to return
Inventory
everywhere instead of<T extends Inventory> T
Opinions? other Ideas?
The text was updated successfully, but these errors were encountered: