1
votes

I was tring to use getDatasID from Factory.java in test.scala.

//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> { ... }

I used two class in scala to run 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])

Both class showed same error.

  1. inferred type arguments [Nothing, Nothing A[Key, Value, [K, V]Datas[Key, Value]]] do not conform ... [K,V,T <: Datas[K,V]]
  2. type mismatch

    • found: class[A[Key, Value, [K,V]Datas[Key, Value]]](classOf[A])
    • required: Class[T]

I would like to match class[Key, Value, Datas[Key, Value]]. The best case will be

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

Above code works.

1

1 Answers

4
votes

Scala can't infer K and V in this situation, you need to supply them explicitly:

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

or

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

(depending on which class you need to pass). If possible, it would be better to change your Java signature, since it doesn't actually use K and V:

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