Proposal: Improved Wildcard Syntax for Java

Joseph D. Darcy Joe.Darcy at Sun.COM
Thu Mar 19 01:45:18 PDT 2009


Hello.

Howard Lovatt wrote:
> 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. 

That sort of trick can only work on types that are reifiable; 
List<List<String>> or something with multiple bounds wouldn't work.  
Also see the checked collections wrapper methods in java.util.Collections.

> 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
>   

That is exactly the sort of issue type systems are designed to catch!

-Joe




More information about the coin-dev mailing list