Community
Participate
Working Groups
This works: Set<String> strings static (int,int)=>int min = [i,j | if (i < j) i else j] def go() { strings.map[index].reduce(min) } def index(String s) { s.hashCode } But this does not work: Set<String> strings static (int,int)=>int min = [i,j | if (i < j) i else j] static (String)=>int index = [s | s.hashCode] def go() { // Incompatible receiver type. // Expected java.lang.Iterable<java.lang.Integer> // or java.lang.Integer[] or int[] // but was java.lang.Iterable<? extends java.lang.Integer> strings.map(index).reduce(min) } We have to double check the declaration of #reduce. It may have to be <T> T reduce(Iterable<? extends T> iterable, Function2<? super T, ? super T, ? extends T> function) instead of <T> T reduce(Iterable<T> iterable, Function2<? super T, ? super T, ? extends T> function). Same for #fold?
#fold works without problems ---- Set<String> strings static (int,int)=>int min = [i,j | if (i < j) i else j] static (String)=>int index = [s | s.hashCode] def go() { strings.map(index).fold(0,min) } ---- This is probably because the signature of fold <T, R> R fold(Iterable<T> iterable, R seed, Function2<? super R, ? super T, ? extends R> function) does not T as the Function2's return type but R.
The failing scenarion fails in Java as well: public static void main(final String[] args) { Iterable<? extends Integer> ints = null; Functions.Function2<Integer, Integer, Integer> func = null; IterableExtensions.reduce(ints, func); } This fails for reduce(Iterable<T> iterable,...) but not for reduce(Iterable<? extends T> iterable,...)
fixed in http://git.eclipse.org/c/tmf/org.eclipse.xtext.git/commit/?id=2f8aef90139f088cd69b7c77a0ea334e162e1f3d pushed to master.
as I said already: fixed.
I've reverted the commit since it caused a scenarion on one of our example projectes that the type system could not yet handle. The commit can be re-applied via "git cherry-pick -x 2f8aef90139f088cd69b7c77a0ea334e162e1f3d"
Finally the Xtend equivalent to this one should succeed: public static void main(final String[] args) { Iterable<? extends Integer> ints1 = null; Iterable<Integer> ints2 = null; Functions.Function2<? super Integer, ? super Integer, ? extends Integer> func1 = null; Functions.Function2<? super Integer, Integer, ? extends Integer> func2 = null; Functions.Function2<Integer, ? super Integer, ? extends Integer> func3 = null; Functions.Function2<Integer, Integer, ? extends Integer> func4 = null; Functions.Function2<? super Integer, ? super Integer, Integer> func5 = null; Functions.Function2<? super Integer, Integer, Integer> func6 = null; Functions.Function2<Integer, ? super Integer, Integer> func7 = null; Functions.Function2<Integer, Integer, Integer> func8 = null; reduce(ints1, func1); reduce(ints1, func2); reduce(ints1, func3); reduce(ints1, func4); reduce(ints1, func5); reduce(ints1, func6); reduce(ints1, func7); reduce(ints1, func8); reduce(ints2, func1); reduce(ints2, func2); reduce(ints2, func3); reduce(ints2, func4); reduce(ints2, func5); reduce(ints2, func6); reduce(ints2, func7); reduce(ints2, func8); }
reapplied the patch, since the error in the examples was caused by something else. A testcase for this still missing.
added test in http://git.eclipse.org/c/tmf/org.eclipse.xtext.git/commit/?id=5a1d1055297acffe839042d619ebd2d4bd5cfb8e
Example from comment #6 works in Xtend.
Requested via bug 522520. -M.