First, I've looked at a lot of previous posts, including:
Java Generics and generic types
Java generics incompatible type
Java Generics Incompatible Types
Java generics: incompatible types
None of them address the issue I'm having. The following code is somewhat contrived, but illustrates the problem. I'm using Java 1.8.
import java.util.List;
/**
* Created by aaron on 11/8/2017.
*/
public abstract class Selecter<STATE extends Selecter.State, ACTION> {
public abstract class State {
public abstract List<ACTION> getActions();
public abstract ACTION selectAction(List<ACTION> list, int selector);
}
public ACTION searchAction(STATE state, int selector) {
return state.selectAction(state.getActions(), selector);
}
}
Now, since STATE
must be a Selector.State
, and getActions()
returns a List<ACTION>
and selectAction()
returns an ACTION
from a List<ACTION>
, why is there an incompatible types error in searchAction()
?
How can I fix it?
EDIT
I think I've fixed it:
import java.util.List;
/**
* Created by aaron on 11/8/2017.
*/
public abstract class Selecter<ACTION> {
public abstract class State<A> {
public abstract List<A> getActions();
public abstract A selectAction(List<A> list, int selector);
}
public ACTION searchAction(State<ACTION> state, int selector) {
return state.selectAction(state.getActions(), selector);
}
}
However, I don't know why this works and the other doesn't.