GWTP
GWTP copied to clipboard
Incorrect generic types for Ordered slot
OrderedSlot
should probably have the following signature:
static class OrderedSlot<T extends PresenterWidget<?> & Comparable<? extends T>> {
}
This is because it is currently impossible to have a common generic baseclass for widgets that need to be members of an OrderedSlot
.
Consider the following example (sorry, it's quite horrible):
interface View {}
abstract class PresenterWidget<V extends View> {}
class OrderedSlot<T extends PresenterWidget<?> & Comparable<T>> {}
interface UiHandlers {}
interface ViewWithUiHandlers<UIH extends UiHandlers> extends View {}
abstract class BasePresenter<UIH extends UiHandlers, V extends ViewWithUiHandlers<UIH>> extends PresenterWidget<V> implements Comparable<BasePresenter<UIH, V>> {}
So the View
, PresenterWidget
, OrderedSlot
, UIHandlers
, ViewWithUiHandlers
emulate existing GWTP classes.
I have added a custom base presenter class BasePresenter
which is generic in the UIHandlers
and the ViewWithUiHandlers
. It also is Comparable
to itself.
Now, in order for another Presenter
to have a Slot
that accepts presenters extending this base class, I would need to following OrderedSlot
declaration:
final OrderedSlot<BasePresenter<?, ?>> slot = new OrderedSlot<>();
But this will result in a compiler error:
/home/boris/.../App.java
Error:Error:line (6)java: type argument com.example.testbench.App.BasePresenter<?,?> is not within bounds of type-variable T
Error:Error:line (6)java: incompatible types: cannot infer type arguments for com.example.testbench.App.OrderedSlot<>
reason: inferred type does not conform to equality constraint(s)
inferred: com.example.testbench.App.BasePresenter<capture#1 of ?,capture#2 of ?>
equality constraints(s): com.example.testbench.App.BasePresenter<capture#1 of ?,capture#2 of ?>,com.example.testbench.App.BasePresenter<?,?>
This is because the compiler cannot find type bounds on the wildcards ?, ?
such that the self type & Comparable<T>
is satisfied. And this makes sense, as if I create two extension classes and add them both to the OrderedSlot<BasePresenter<?, ?>>
class PresenterOne extends BasePresenter<SomeUiHandlers, ViewWithUiHandlers<SomeUiHandlers>> {}
class PresenterTwo extends BasePresenter<OtherUiHandlers, ViewWithUiHandlers<OtherUiHandlers>> {}
Then as PresenterOne implements Comparable<BasePresenter<SomeUiHandlers, ViewWithUiHandlers<SomeUiHandlers>>
and PresenterTwo implements Comparable<BasePresenter<OtherUiHandlers, ViewWithUiHandlers<OtherUiHandlers>>
the type equality constraint is violated.
In order for this situation to work, the type constraint on OrderedSlot
needs to be relaxed.
Would you like to submit a pull request to fix that issue?
I'm facing this issue right now.
:+1: to this feature
@olafleur sorry; haven't had much time recently - I'll try and push something before next week.
No worries. If you need help in the process, just ping us here!