ArrayFactory SAM type / toArray

Joshua Bloch josh at bloch.us
Wed Sep 19 17:11:43 PDT 2012


David,


On Wed, Sep 19, 2012 at 4:45 PM, David M. Lloyd <david.lloyd at redhat.com>wrote:

>
> It should be:
>
>   <S super T> S[] toArray(Class<S> clazz);
>
>
Actually we considered and rejected that parameterization  back in '03 (if
memory serves).  Sometimes the client knows more about the contents of the
array than the compiler does.  So, for example, suppose you know that a
Collection<Number> contains only Integers.  Then you might write:

    private static final Integer[] EMPTY_INTEGER_ARRAY = new Integer[0];
    ...

    Collection<Number> c = ... ;

    ...

    Integer[] a = c.toArray(EMPTY_INTEGER_ARRAY);

The type system can't prove that the call won't result in an
ArrayStoreException, but you (the programmer) know that it won't.  Perhaps
we made the wrong decision, but it was a conscious decision.

     Josh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/lambda-libs-spec-experts/attachments/20120919/47e37a26/attachment.html 


More information about the lambda-libs-spec-experts mailing list