lambda inference bug

Rémi Forax forax at univ-mlv.fr
Sat Sep 3 08:06:24 PDT 2011


Hi Maurizio, hi all,
The code below doesn't compile but I think it should.

[lambda.javac] ReducerBug.java:17: error: bad operand types for binary 
operator '+'
[lambda.javac]     int result = reduce(l, 0, #{ e, v -> e + v});
[lambda.javac]                                            ^
[lambda.javac]   first type:  Integer
[lambda.javac]   second type: V
[lambda.javac]   where V is a type-variable:
[lambda.javac]     V extends Object declared in interface Reducer
[lambda.javac] 1 error

I don't understand why V (which is a type variable of the interface) can 
be propagated
in the body of the lambda thus I think it's a bug.

Rémi

public class ReducerBug {
   interface Reducer<E, V> {
     public V reduce(E element, V value);
   }

   private static <E> int reduce(Iterable<? extends E> iterable, int 
initialValue, Reducer<? super E, Integer> reducer) {
     int value = initialValue;
     for(E e: iterable) {
       value = reducer.reduce(e, value);
     }
     return value;
   }

   public static void main(String[] args) {
     java.util.List<Integer> l = java.util.Arrays.asList(1, 2, 3);
     int result = reduce(l, 0, #{ e, v -> e + v});
     System.out.println(result);
   }
}


More information about the lambda-dev mailing list