Skip to content

Instantly share code, notes, and snippets.

@octylFractal
Last active August 29, 2015 14:24
Show Gist options
  • Save octylFractal/04341ce6e802143a6383 to your computer and use it in GitHub Desktop.
Save octylFractal/04341ce6e802143a6383 to your computer and use it in GitHub Desktop.
Foo foo = new AutoValue_Foo.Builder().list(new ArrayList<String>()).build();
List<String> listNoWildcard = (List<String>) foo.list();
List<? extends String> listWithWildcard = foo.list();
Stream<? extends string> s = listWithWildcard.stream();
// alright, let's reduce this
s.reduce("", new BinaryOperator<String>() {
// error: The method reduce(capture#3-of ? extends String, BinaryOperator<capture#3-of ? extends String>)
// in the type Stream<capture#3-of ? extends String> is not applicable for the arguments
// (String, new BinaryOperator<String>(){})
@Override
public String apply(String t, String u) {
return t + u;
}
});
// ok, let's satisfy that
s.reduce("", new BinaryOperator<? extends String>() {
// error: The type new BinaryOperator(){} cannot extend or implement BinaryOperator<? extends String>.
// A supertype may not specify any wildcard
@Override
public String apply(String t, String u) {
return t + u;
}
});
// And that is impossible to fix, so I need to cast
((Stream<String>) s).reduce("", new BinaryOperator<String>() {
@Override
public String apply(String t, String u) {
return t + u;
}
});
// unchecked casts are not good...but that's the only option
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment