Java generics: Related mismatch: type is not a valid substitute for a limited type parameter

I ran into the following problem:

I have class and interface definitions

public abstract class ViewModelRefreshPostListFragment<T extends IRefreshPostViewCallback, R extends RefreshPostViewModel<T>> extends RefreshPostListFragment implements IRefreshPostView { private final ViewModelHelper<T, R> mViewModeHelper = //error here new ViewModelHelper<>(); ... } public abstract class RefreshPostViewModel<R1 extends IRefreshPostViewCallback> extends AbstractViewModel<IRefreshPostViewCallback> {} public class ViewModelHelper<T extends IView, R extends AbstractViewModel<T>> {} public abstract class AbstractViewModel<T extends IView> {} public interface IRefreshPostViewCallback extends IView {} 

Eclipse gives me this error: Associated mismatch: Type R not a valid replacement for the limited parameter <R extends AbstractViewModel<T>> type ViewModelHelper<T,R>

Based on Java inheritance, I created these two chains:

The chain from the definition of the ViewModelRefreshPostListFragment class
1) R extends RefreshPostViewModel<T>R extends RefreshPostViewModel<R1 extends IRefreshPostViewCallback>R extends AbstractViewModel<IRefreshPostViewCallback>
1.1) T extends IRefreshPostViewCallback
1.2) T (from RefreshPostViewModel<T> ) is replaced by <R1 extends IRefreshPostViewCallback> specific result is from 1.1) and 1.2), so the parameter T should be in order.

Chain from ViewModelHelper Class Definition
2) R extends AbstractViewModel<T>
2.1) T extends IView IRefreshPostViewCallback extends IView , IRefreshPostViewCallback extends IViewT can be replaced with IRefreshPostViewCallback

If I apply 2.1) to 1.1) && 1.2), we see that the parameter T is consistent

From 1) it follows R extends AbstractViewModel<IRefreshPostViewCallback> from 2) it follows R extends AbstractViewModel<T> , and from 2.1) it follows that T can be replaced by IRefreshPostViewCallback , If I understand correctly, this error should not appear, can someone explain me, why does the eclipse give me an error ??

Thanks!

+6
source share
1 answer

The error message occurs because R not within its boundaries.

Your ViewModelHelper class extends AbstractViewModel<IRefreshPostViewCallback> , regardless of what R1 really is.

In the ViewModelHelper class, change the type argument in the extends AbstractViewModel clause to R1 instead of IRefreshPostViewCallback .

 public abstract class RefreshPostViewModel<R1 extends IRefreshPostViewCallback> extends AbstractViewModel<R1> 

and this will fix the error.

This will result in the correct T in the ViewModelHelper . Instead of R will be RefreshPostViewModel<IRefreshPostViewCallback> , you will use RefreshPostViewModel<T> , having fulfilled borders.

+4
source

Source: https://habr.com/ru/post/988541/


All Articles