Question

I am trying to write a interfaces and default implementations of the MVP pattern. However, I am receiving an error message on something that I think should be possible. The error message that I receive is:

required: CAP#1
  found: MVPPresenter<M,V>
  reason: actual argument MVPPresenter<M,V> cannot be converted to CAP#1 by method invocation conversion
  where V,M,P are type-variables:
    V extends IMVPView< ? extends IMVPPresenter< ?,V>> declared in class MVPPresenter
    M extends Object declared in class MVPPresenter
    P extends IMVPPresenter< ?,? extends IMVPView< P>> declared in interface IMVPView
  where CAP#1 is a fresh type-variable:
    CAP#1 extends IMVPPresenter< ?,V> from capture of ? extends IMVPPresenter< ?,V>

This does not make sense to me because MVPPresenter should be a CAP#1. Can someone please explain why I can't do this or provide a way to fix the problem?

/**
 * Interface for the presenter in MVP
 *
 * @param <M> Model type
 * @param <V> View type
 */
public interface IMVPPresenter<M, V extends IMVPView<? extends IMVPPresenter<?, V>>> {
    ...
}

/**
 * Interface for the view in MVP
 *
 * @param <P> Presenter type
 */
public interface IMVPView<P extends IMVPPresenter<?, ? extends IMVPView<P>>> {
    ...
    public void setPresenter(P presenter);
}

/**
 * Default implementation of the presenter interface
 *
 * @param <M> Model Type
 * @param <V> View Type
 */
public class MVPPresenter<M, V extends IMVPView<? extends IMVPPresenter<?, V>>>
    implements IMVPPresenter<M, V> {
    ...
    protected void setView(V view) {
        ...
        view.setPresenter(this); // Error on this line
    }
}
Was it helpful?

Solution

Concrete classes must be … well a bit more concrete.

public class MVPPresenter<M, V extends IMVPView<MVPPresenter<M,V>>>
  implements IMVPPresenter<M, V> {

  protected void setView(V view) {
    view.setPresenter(this); // No more error on this line
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top