lambda inference bug

maurizio cimadamore maurizio.cimadamore at oracle.com
Mon Sep 5 01:20:55 PDT 2011


Hi,
this looks like a type-substitution bug - however, I don't think this 
should compile (w/ or w/o the bug), given that there's no lower bound on 
E the parameter types of the lambda body cannot be instantiated 
(therefore the lambda expression cannot be attributed). This should be a 
cyclic inference error (as inference is stuck in this case).

The experimental option -XDcomplexinference should help unstuck this 
(but will infer Object for E).

Maurizio

On 03/09/2011 16:06, Rémi Forax wrote:
> 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