简体   繁体   English

在 Scala 中使用 Java 类的有界通配符的编译错误

[英]Compilation error with bounded wildcards using Java classes in Scala

In Java, we have defined an ObservableCollection.java like this:在 Java 中,我们定义了一个ObservableCollection.java如下所示:

public class ObservableCollection<T> implements Collection<T> {

   public SubscriptionHandle onElementAdded(Consumer<T> onAdded) {
     // ... 
   }
}

And an AgentService.java that returns an ObservableCollection:AgentService.java返回一个ObservableCollection:

public interface AgentService {

    ObservableCollection<? extends Agent> getAgents();

}

Now, I am trying to use this ObservableCollection.java in a Scala project like this:现在,我试图在像这样的Scala项目中使用这个ObservableCollection.java

  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:编辑:使用Cosumer[Agent]导致以下错误:

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

The thing is not in Scala-Java interop.事情不在 Scala-Java 互操作中。 The following Scala code doesn't compile either以下 Scala 代码也无法编译

import java.util.function.Consumer
import java.util

trait Agent
trait SubscriptionHandle
trait AgentService {
  def getAgents: ObservableCollection[_ <: Agent]
}
trait ObservableCollection[T] extends util.Collection[T] {
  def onElementAdded(onAdded: Consumer[T]): SubscriptionHandle
}

def test(service: AgentService): Unit = {
  val onAdded: Consumer[_ <: Agent] = ???
  val agents: ObservableCollection[_ <: Agent] = service.getAgents
  agents.onElementAdded(onAdded)
//                      ^^^^^^^
}

//type mismatch;
// found   : java.util.function.Consumer[_$2] where type _$2 <: App.Agent
// required: java.util.function.Consumer[_$3]

You misuse existential types (wildcard generics).您滥用了存在类型(通配符泛型)。 The following code can't compile以下代码无法编译

trait X[T]
trait Y[T] {
  def foo(x: X[T]) = ???
}
val x: X[_] = ???
val y: Y[_] = ???
y.foo(x) // doesn't compile

Both x and y have existential types but foo accepts x of type X[T] , where T must be the same as T in the type of y , ie Y[T] , so you can't guarantee that T are the same.这两个xy有存在的类型,但foo接受x型的X[T]其中T必须是一样T的类型y ,即Y[T]所以你不能保证T是相同的。

One way to fix compilation is to add generics to AgentService修复编译的一种方法是向AgentService添加泛型

trait Agent
trait SubscriptionHandle
trait AgentService[T <: Agent] {
  def getAgents: ObservableCollection[T]
}
trait ObservableCollection[T] extends util.Collection[T] {
  def onElementAdded(onAdded: Consumer[T]): SubscriptionHandle
}

def test[T <: Agent](service: AgentService[T]): Unit = {
  val onAdded: Consumer[T] = ???
  val agents: ObservableCollection[T] = service.getAgents
  agents.onElementAdded(onAdded)
}

or to its method或者到它的方法

trait Agent
trait SubscriptionHandle
trait AgentService {
  def getAgents[T <: Agent]: ObservableCollection[T]
}
trait ObservableCollection[T] extends util.Collection[T] {
  def onElementAdded(onAdded: Consumer[T]): SubscriptionHandle
}

def test[T <: Agent](service: AgentService): Unit = {
  val onAdded: Consumer[T] = ???
  val agents: ObservableCollection[T] = service.getAgents
  agents.onElementAdded(onAdded)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM