I'm using Java with vavr and I have a Function1 that accepts a Seq<Parent>
and returns a String
. Why won't that class accept Seq<Child>
? It seems like something with contravariance/covariance/invariance, but I am having trouble working through why it won't compile. Here is my code:
interface Bar {
String getFoo();
}
class Foo implements Bar {
private String foo;
private String bar;
@Override
public String getFoo() {
return foo;
}
public Foo(String foo, String bar) {
this.foo = foo;
this.bar = bar;
}
}
class Scratch {
public static void main(String[] args) {
Function1<Seq<Bar>, String> myFunction = (it) -> "foo";
Foo obj = new Foo("foo", "bar");
Foo obj1 = new Foo("hello", "world");
Seq<Foo> list = List.of(obj, obj1);
String result = myFunction.apply(list); // IntelliJ says this line is incorrect
}
}
Instead, I get the error (from IntelliJ):
apply (io.vavr.collection.Seq<Bar>) in Function1 cannot be applied to (io.vavr.collection.Seq<Foo>).
This simple example seems like it should work just fine. What am I missing and why doesn't it work?