In Java, we have defined an ObservableCollection.java
like this:
public class ObservableCollection<T> implements Collection<T> {
public SubscriptionHandle onElementAdded(Consumer<T> onAdded) {
// ...
}
}
And an AgentService.java
that returns an ObservableCollection:
public interface AgentService {
ObservableCollection<? extends Agent> getAgents();
}
Now, I am trying to use this ObservableCollection.java
in a Scala project like this:
def test(service: AgentService): Unit = {
val onAdded: Consumer[_ <: Agent] = ???
service.getAgents.onElementAdded(onAdded)
}
Trying this results in the following compilation error:
type mismatch;
found : java.util.function.Consumer[_$1] where type _$1 <: com.xxxx.xx.xx.agent.Agent
required: java.util.function.Consumer[?0] where type ?0 <: com.xxxx.xx.xx.agent.Agent
service.getAgents.onElementAdded(onAdded)
^
one error found
This does not make much sense to me. Is there a way I can get this running?
Edit: Using a Cosumer[Agent]
results in the following error:
type mismatch;
found : java.util.function.Consumer[com.xxxx.xx.xx.agent.Agent]
required: java.util.function.Consumer[?0] where type ?0 <: com.kuka.cc.si.agent.Agent
Note: com.xxxx.xx.xx.agent.Agent >: ?0, but Java-defined trait Consumer is invariant in type T.
You may wish to investigate a wildcard type such as `_ >: ?0`. (SLS 3.2.10)
service.getAgents.onElementAdded(onAdded)
^
one error found