1
votes

I have legacy (no modification allowed) Java code:

//File Foo.java
package sof;
public interface Foo<T> {}

//File Bar.java
package sof;
public class Bar implements Foo<String> {}

//File Holder.java    
package sof;
import java.util.List;
public class Holder {
    private List<Foo<?>> lst;
    public Holder() {}
    public Holder(List<Foo<?>> lst) {this.lst = lst;}
    public List<Foo<?>> getLst() {return lst;}
    public void setLst(List<Foo<?>> lst) {this.lst = lst;}
}

Now, in my Scala code I want to pass List of bars to holder:

import sof._
import collection.JavaConverters._
val bar = new Bar()
val bars: java.util.List[Bar] = List(bar).asJava
val holder = new sof.Holder(bars)

It won't compile. The error is:

Error:(37, 18) type mismatch; found : java.util.List[sof.Bar] required: java.util.List[sof.Foo[_]] Note: sof.Bar <: sof.Foo[_], but Java-defined trait List is invariant in type E. You may wish to investigate a wildcard type such as _ <: sof.Foo[_]. (SLS 3.2.10)
new sof.Holder(bars)

As a workaround, I can

val holder = new sof.Holder()
val tmpLst = holder.getLst
tmpLst.add(bar)

It works but it's awful. So, what's the Scala structure that fits java.util.List<?>?

1
The problem is not converting the Scala List into the Java one. You already have that. The problem is that because Java doesn't provide control over variance a j.u.List<Bar> is not a subtype of a j.u.List<Foo<?>>. You may try the up casting before the call to asJava something like this should work: val bars = (List(bar) : List[String]).asJava - Luis Miguel Mejía Suárez
You don't have any java.util.List<?> in your code or error. - Jasper-M
@LuisMiguelMejíaSuárez Unfortunately, it ends up with "Cannot upcast List[Bar] to List[String]" compilation error. And yes, it makes sense: indeed, a list of Bar is not a list of String. - Lopotun
@Lopotun O, sorry a typo, I was in mobile. It should have been val bars = (List(bar) : List[Foo[String]]).asJava Because a Bar is Foo[String]. - Luis Miguel Mejía Suárez
@Jasper-M I'm afraid I didn't quite understand your answer. The given Java code is legacy and I cannot change it. - Lopotun

1 Answers

3
votes

Java wants java.util.List<Foo<?>> but you are giving it java.util.List<Bar>. Java does not support List variance so this does not work.

You need to give it the actual type it wants:

val bar = new Bar()
val foos: List[Foo[_]] = List(bar)
val holder = new sof.Holder(foos.asJava)