tags:

views:

79

answers:

1

How can I pass a parameter to a Java method with a raw type in its method signature? My example API is as follows:

class P<T> {}

class Q {
    public void f(P p[]) {}
}

And my attempt to call it from Scala looks like this:

val q = new Q()
val p = new P()
val p_array = Array(p)
q.f(p_array)

Which generates the following compiler error:

type mismatch;
found   : Array[P[Nothing]]
required: Array[P[_]]
q.f(p_array)

Parameterizing p as type P[Any] doesn't help either. I'm using 2.8 RC6.

By way of background, the API which has caused me this problem is something called org.teleal.cling.model.meta.LocalDevice The constructor looks like it takes a parameterized argument 'deviceServices'. However, the type parameter 'LocalService' also takes a parameter, not supplied, resulting in the awkward method signature.

+3  A: 

You can simply make the array instantiation explicit (shown in compilable form):

object PQ {
    def
    main(args: Array[String]): Unit = {
        val q = new Q()
        val p = new P()
        val p_array = Array[P[_]](p)
        q.f(p_array)
    }
}
Randall Schulz
Thanks! I'll have to puzzle over what that means, but much better to puzzle over a working example than a broken one!
Crosbie Smith