Autoboxing vs wildcards

Neal Gafter neal at gafter.com
Thu May 22 20:02:21 PDT 2008


Offhand, I'd say this should probably compile.  I'll look at it.  You can
always resort to explicit type arguments as a workaround, e.g.
Reduce.<Integer>reduce(...).

On Thu, May 22, 2008 at 5:45 PM, Mark Mahieu <mark at twistedbanana.demon.co.uk>
wrote:

> 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 + i2});
>    }
> }
>
>
> Reduce.java:21: <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 + i2});
>                     ^
> 1 error
>
>
> Regards,
>
> Mark
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/closures-dev/attachments/20080522/5cf3a60b/attachment.html 


More information about the closures-dev mailing list