Autoboxing vs wildcards

Mark Mahieu mark at
Thu May 22 17:45:30 PDT 2008

This class looks like it could compile to me, but the third call to  
the 'reduce' method isn't considered valid unless I remove the  
wildcard from the method's signature, but of course that then means  
that the first call won't compile.

Is there something I'm not seeing here?

import java.util.*;

public class Reduce {

     static <T> T reduce(Iterable<? extends T> values, T initial,  
{T,T=>T} reducer) {
         T result = initial;
         for (T value : values) {
             result = reducer.invoke(result, value);
         return result;

     public static void main(String[] args) {

         List<Integer> values = Arrays.asList(1, 2, 3, 4, 5);

         Integer initial = 0;

         Number num = reduce(values, initial, {Number n1, Number n2  
=> n1.intValue() > n2.intValue() ? n1 : n2});
         Integer i1 = reduce(values, initial, {Integer i1, Integer i2  
=> i1 + i2});
         Integer i2 = reduce(values, initial, {int i1, int i2 => i1 +  
} <T>reduce(java.lang.Iterable<? extends T>,T,{T,T =>  
T}) in Reduce cannot be applied to  
(java.util.List<java.lang.Integer>,java.lang.Integer,{int,int => int})
         Integer i2 = reduce(values, initial, {int i1, int i2 => i1 +  
1 error



More information about the closures-dev mailing list