【问题标题】:scala mismatch & inferred type arguments with Javascala不匹配和推断类型参数与Java
【发布时间】:2016-07-02 03:40:27
【问题描述】:

我试图在test.scala 中使用Factory.java 中的getDatasID

//Factory.java
class Factory {
...
    public <K, V, T extends Datas<K, V>> DatasID<T> getDatasID(Class <T> dataClass) ...
}

// C.java
public class C extends Datas<Key, Value> { ... }

我在 scala 中使用了两个类来运行 getDatasID

//Test.scala
abstract class A[K, V, T[K, V] <: Datas[K, V]]
abstract class B extends Datas[Key, Value]
val targetA = new Factory()
  .getDatasID(
    classOf[A
        [Key, Value, ({type T[K, V]=Datas[Key, Value})#T] 
])

val targetB = new Factory()
  .getDatasID(classOf[B])

两个类都显示相同的错误。

  1. 推断的类型参数[Nothing, Nothing A[Key, Value, [K, V]Datas[Key, Value]]] 不符合... [K,V,T &lt;: Datas[K,V]]
  2. 类型不匹配

    • 找到:class[A[Key, Value, [K,V]Datas[Key, Value]]](classOf[A])
    • 必填:Class[T]

我想匹配类[Key, Value, Datas[Key, Value]]。最好的情况是

val targetB = new Factory()
  .getDatasID(classOf[B])

以上代码有效。

【问题讨论】:

    标签: java scala


    【解决方案1】:

    Scala 在这种情况下无法推断出KV,您需要明确地提供它们:

    new Factory.getDatasId[Key, Value, B](classOf[B])
    

    new Factory.getDatasId[Key, Value, A[Key, Value, _ <: Datas[Key, Value]](classOf[A])
    

    (取决于您需要通过的课程)。如果可能,最好更改您的 Java 签名,因为它实际上并没有使用 KV

    public <T extends Datas<?, ?>> DatasID<T> getDatasID(Class <T> dataClass)
    

    【讨论】:

    • 非常感谢!!很有帮助!
    • 我刚刚在问题的java文件中添加了class C。如果我需要getDatasIDclass C 怎么办?
    • @MiaeKim 和class B一模一样。
    • 谢谢!现在我明白了。
    猜你喜欢
    • 2015-12-31
    • 2012-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-30
    • 1970-01-01
    • 2017-06-29
    • 1970-01-01
    相关资源
    最近更新 更多