sockeqwe
sockeqwe

Reputation: 15929

Generic Type argument is not within bounds

I'm working on a little demo android app where I mix some java classes and interfaces with kotlin. I want to extend from a java class with generic type arguments called MvpViewStateActivity:

abstract class MvpViewStateActivity<V extends MvpView, P extends MvpPresenter<V> > { ... }

where MvpView is just an empty java interface:

interface MvpView { ... }

and MvpPresenter is an java interface like this:

interface MvpPresenter<V extends MvpView> { ... }

and SearchViewActivity (Java):

class SearchActivity extends MvpViewStateActivity<SearchView, SearchPresenter>
                     implements SearchView { ... }

So now I create a kotlin interface SearchView:

interface SearchView : MvpView { 
    fun setItems( items : List<Item> )
 }

where Item is a pojo data class.

SearchPresenter is written in kotlin as well:

interface SearchPresenter : MvpPresenter<SearchView> { ... }

So to sum it up:

Java classes:

Kotlin classes / interfaces

When I try to compile that code I get the following error:

error: type argument SearchView is not within bounds of type-variable V where V is a type-variable: V extends MvpView declared in class MvpViewStateActivity

But SearchView : MvpView (in other words SearchView extends MvpView).

The compiler points to that line of code:

SearchActivity extends MvpViewStateActivity<SearchView

Am I overlooking something? Is this a kotlin bug?

Upvotes: 0

Views: 6720

Answers (1)

vanOekel
vanOekel

Reputation: 6548

I cannot reproduce the "not within bounds" issue.

This is the first time I use Kotlin, so let me explain what I did so you can verify. I changed the class-names for clarity.

Using Java 7, Eclipse, create a Java project with a package "generics". Create two files:

package generics;
public interface IView {}

and

package generics;
public interface IPresenter<VIEW extends IView> {}

Download Kotlin compiler version 1.0.0-beta-2423, extract and copy IView.class and IPresenter.class from the project "bin\generics" folder into a new "kotlin\generics" folder. Create KGenericViewPresenter.kt in the "kotlin\generics" folder:

package generics

interface ISearchView : IView {}
interface ISearchPresenter : IPresenter<ISearchView> {}

Run:
bin\kotlinc -cp . generics\KGenericViewPresenter.kt

Back in the Java project, add class folder "fromkotlin", create a "generics" folder in it and copy ISearchView.class and ISearchPresenter.class from the "kotlin\generics" folder.
Refresh the Java project (so that the two new classes from Kotlin are picked up).
Create two new files in the Java project:

package generics;
public abstract class AbstractViewStateActivity<VIEW extends IView, PRESENTER extends IPresenter<VIEW>> {}

and

package generics;
public class ViewStateActivity extends AbstractViewStateActivity<ISearchView, ISearchPresenter> 
    implements ISearchView {}

Eclipse does not complain, the project's "bin\generics" folder contains files AbstractViewStateActivity.class and ViewStateActivity.class.

If this is a valid test, try refreshing your IDE environment and also make sure there is no class in the package with the name V or P (also check unit-test classes).

Upvotes: 0

Related Questions