I'm confused by a bit of Java 8's type inference. The following code:
private static <T> Function<Iterable<? extends T>, Iterator<? extends T>> toIterator() { return Iterable<? extends T>::iterator; } breaks with the compile error
error: incompatible types: invalid method reference return Iterable<? extends T>::iterator; ^ method iterator in interface Iterable<T#2> cannot be applied to given types required: no arguments found: Iterable<? extends T#1> reason: actual and formal argument lists differ in length where T#1,T#2 are type-variables: T#1 extends Object declared in method <T#1>toIterator() T#2 extends Object declared in interface Iterable whereas removing the explicit generic
private static <T> Function<Iterable<? extends T>, Iterator<? extends T>> toIterator() { return Iterable::iterator; } works, as does the old-school anonymous inner class
private static <T> Function<Iterable<? extends T>, Iterator<? extends T>> toIterator() { return new Function<Iterable<? extends T>, Iterator<? extends T>>() { @Override public Iterator<? extends T> apply(Iterable<? extends T> iterable) { return iterable.iterator(); } }; } Can anyone suggest what might be going on here?
<? extends T>wildcards are different and won't consider the possibility that the iterable is the argument.::shouldn't have type arguments. Actually...I'm going to review this statement.Iterable<? extends T>::iterator;