Java
Set<Long> set = new HashSet<Long>();
set.add(100);
long x = 2;
foo(x, set);
Scala
def foo(a: Long, b: java.util.Set[Long])
Error:
could not parse error message:
required: long,Set<Object>
found: long,Set<Long>
reason: actual argument Set<Long> cannot be converted
to Set<Object> by method invocation conversion
Then, I modified my Java code to resolve the compile-time error.
Set<Object> set = new HashSet<Object>();
However, the resolution of the compile-time error came at the expense of type safety. How can I properly resolve the above error?
EDIT
After @som-snytt resolved my issues here, I ran into this problem. I don't think it's the same question since , in my linked question, using, in Scala, foo(Long, java.util.Set[Long]) worked when calling (from Java) ScalaObject.foo(long, Set[Long])
def foo(a: Long, b: java.util.Set[java.lang.Long])should get rid of the compilation error.foo(x: Long, set: java.util.Set[java.lang.Long] = { val y: scala.collection.immutable.Set[Long] = set.asScala.toSetis giving me:[INFO] found : scala.collection.immutable.Set[Any] [INFO] required: scala.collection.immutable.Set[Long]Longs tojava.lang.Longor importJavaConvertersin foo's body as noted in the answer below.