I'm trying to convert a project over to scala 2.8 from 2.7 and I've run into some difficulty in the code that interacts with Java. Below is a slightly convoluted piece of sample code displaying the problem. Essentially I have class with a member variable of type mutable.Map[K,V] and I can't find a way to pass that through to a method that expects a java.util.Map[K,V]. Any help would be great.
scala> import scala.collection.JavaConversions._
import scala.collection.JavaConversions._
scala> class A[K,V](m : collection.mutable.Map[K,V]) {
| def asJava : java.util.Map[K,V] = m
| }
<console>:9: error: could not find implicit value for parameter ma: scala.reflect.ClassManifest[K]
def asJava : java.util.Map[K,V] = m
I don't know why you are trying to repeat the conversion from JavaConversions, but i think you can do it with adding the implicit parameter ma requested:
import scala.collection.JavaConversions._
class A[K,V](m : collection.mutable.Map[K,V]) {
def asJava(implicit ma:ClassManifest[K]) : java.util.Map[K,V] = m
}
From console
scala> import scala.collection.JavaConversions._
class A[K,V](m : collection.mutable.Map[K,V]) {
def asJava(implicit ma:ClassManifest[K]) : java.util.Map[K,V] = m
}
import scala.collection.JavaConversions._
scala>
defined class A
scala> val map=scala.collection.mutable.HashMap[Int, Int]()
map: scala.collection.mutable.HashMap[Int,Int] = Map()
scala> map += 0->1
res3: map.type = Map(0 -> 1)
scala> val a=new A(map)
a: A[Int,Int] = A@761635
scala> a.asJava
res4: java.util.Map[Int,Int] = {0=1}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With