Request for review : 7121314 : Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec (original) (raw)
David Holmes david.holmes at oracle.com
Wed Mar 14 05:15:23 UTC 2012
- Previous message: Request for review : 7121314 : Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
- Next message: RFR : 7148584 Jar tools fails to generate manifest correctly when boundary condition hit
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 14/03/2012 2:49 PM, Eamonn McManus wrote:
Why don't we have public T[] toArray(T[] a) ? This would prevent from the cast r[i] = (T)it.next();
It's too late to change the method signature now. Sorry about my english, I meant why don't we have had ... In other words, has there been a reason that it was not like that from the beginning? Perhaps because it is not legal Java?
Thanks Eamonn!
That's ironic. :) In "The Java Programming Language" 3rd edition, Section 11.3 Generic Methods and Constructors, I use toArray as an example of a generic method. I then pose the question "Shouldn't there be some restriction between T and E as they must be compatible?", the answer to which is "logically there could be" but "there is no way to express this restriction. Only wildcards can be given a lower type bound". I then continue to point out that such a restriction is not strictly necessary as you might have a List, for example, that you only stored Strings into, so why shouldn't you be able to pass in a String[] to toArray? The runtime array-store checks will catch any unsuitable array.
Cheers, David
Éamonn
On 13 March 2012 12:16, Ulf Zibis<Ulf.Zibis at gmx.de> wrote:
Am 10.03.2012 13:52, schrieb David Holmes:
On 10/03/2012 12:02 PM, Ulf Zibis wrote:
Why don't we have public T[] toArray(T[] a) ? This would prevent from the cast r[i] = (T)it.next();
It's too late to change the method signature now. Sorry about my english, I meant why don't we have had ... In other words, has there been a reason that it was not like that from the beginning? Wouldn't following statement potentially throw a ClassCastException ? r[i] = (T)it.next(); Apparently not. I passed in a String[] when it should be Object[] and got ArrayStoreException. Checking the bytecode I don't see a checkcast. Thanks, checking that out. -Ulf
- Previous message: Request for review : 7121314 : Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
- Next message: RFR : 7148584 Jar tools fails to generate manifest correctly when boundary condition hit
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]