ArrayFactory SAM type / toArray

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


On Wed, Sep 19, 2012 at 4:45 PM, David M. Lloyd <david.lloyd at>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.


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