I have this code:
import java.util.ArrayList;
import java.util.List;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
class B { }
class C extends B { }
class D { }
class Test {
void test() {
List<B> lb = new ArrayList<B>();
List<C> lc = new ArrayList<C>();
Iterable<B> it = lb == null ? lc : lb; // a
FluentIterable.from(lb == null ? lc : lb).transform(new Function<B, D>() { // b
@Override
public D apply(B b) {
return null;
}
});
}
}
Under Java 8 line //b gives me these compiler errors:
Incompatible types. Found: 'java.util.List<C>', required: 'java.util.List<capture<? extends B>>'
Incompatible types. Found: 'java.util.List<B>', required: 'java.util.List<capture<? extends B>>'
Under Java 6 the same line compiles fine.
Line //a
Iterable<B> it = lb == null ? lc : lb;
produces compile error
Incompatible types. Found: 'java.util.List<C>', required: 'java.lang.Iterable<B>'
under both Java 6 and Java 8, which is correct.
But Guava's FluentIterable.from is just a wrapper around Iterable. Why does it not produce any error under Java 6 and does produce errors under Java 8? How does it differ from what I have at line //a?
Thank you.