Proposal: Improved Wildcard Syntax for Java
Howard Lovatt
howard.lovatt at iee.org
Tue Mar 17 16:29:25 PDT 2009
Hi Joe,
Emails overlapped, you sent me one whilst I was replying to you, hence
second email.
2009/3/18 Joseph D. Darcy <Joe.Darcy at sun.com> wrote:
[snip]
>> There is only one change to the type system; which is to allow
>> covariant assignment, e.g. List<Object> lo = new ArrayList<String>().
>> This isn't a drastic change, arrays already do this.
>
> This is in general *not* type safe. Arrays allow one to do this because
> array types are reified and there is a store-check at runtime when writing
> into an array. Since generics in Java are implemented via erasure, this
> implementation strategy is not possible for Lists and Sets, etc.
In the example I sent to Neal I showed how you can write a List that
generates an ArrayStoreException, that still uses erasure, by using a
factory (java.lang.reflect.Array.newInstance( ... )) to generate a
correctly typed array. The underlying array generates the
ArrayStoreException. Therefore it is possible to have generic
covariant assignment, erasure, and runtime type safety simultaneously.
Trying to catch this error, incorrect use of an aliased generic, in
the type system isn't worth the trouble, arrays have a more useful
behaviour. Note this proposed change will also remove the impedance
mismatch between arrays and collections.
More information about the coin-dev
mailing list