Java Forum / General / January 2008
generic arrays
Roedy Green - 01 Jan 2008 11:20 GMT Why does Java have a problem with:
// You cannot use "new" to allocate an array of T where T is a generic type, e.g. <T>. // Recall that even if it did, it would still allocate an array of Objects at run time. T[] things = new T[10];
Why does it not just generate the code for:
Object[] things = new Object[10];
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Joshua Cranmer - 01 Jan 2008 16:19 GMT > Why does Java have a problem with: > [quoted text clipped - 3 lines] > Objects at run time. > T[] things = new T[10]; The short answer: Generics in Java, at present, is almost entirely a compile-time hack. Anything which would have to be passed down into runtime cannot be done without warning (e.g., casting, instanceof, new).
> Why does it not just generate the code for: > > Object[] things = new Object[10]; We were promised that things would be an array of T's. Communication into the outside world would pass the runtime array store check (i.e., the array is initialized as an Object[]) but would violate the type safety.
public class GenericTest<T> { private T[] array;
public GenericTest(int count) { array = new T[count]; }
public void set(int place, T x) { array[place] = x; }
public T get(int place) { return array[place]; }
public void malicious(Object insertee) { Object[] evil = (Object[])array; evil[0] = insertee; }
public static void main(String... args) { GenericTest<String> test = new GenericTest<String>(1); test.malicious(new Object()); System.out.println(test.get(0) instanceof String); } }
Should new arrays be legal, this code would be forced to run with unexpected output. The array is of type Object[], so inserting an Object does not cause an ArrayStoreException when it should--it needs to be of type T.
The process by which this issue would be fixed is called reification--pushing generics down to the level of runtime, probably with the addition of a few instructions to the JVM (anewarray, new, and similar instructions having variants that pop classes from the stack is my bet). It is also, IMO, the most needed change to Java.
 Signature Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth
Alexander.V.Kasatkin@gmail.com - 01 Jan 2008 20:54 GMT > > Why does Java have a problem with: The shortest answer is "use the ArrayList instead" :))))))))
Roedy Green - 01 Jan 2008 23:48 GMT On Tue, 1 Jan 2008 12:54:15 -0800 (PST), Alexander.V.Kasatkin@gmail.com wrote, quoted or indirectly quoted someone who said :
>The shortest answer is "use the ArrayList instead" :)))))))) The problem has shown up in 4 sorts. I can't get way with an ArrayList
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Roedy Green - 01 Jan 2008 23:46 GMT On Tue, 01 Jan 2008 16:19:36 GMT, Joshua Cranmer <Pidgeot18@verizon.invalid> wrote, quoted or indirectly quoted someone who said :
> It is also, IMO, the most needed change to Java. What sort of changes would happen to the syntax? The problem now is generics are so complicated they are more likely to introduce rather than catch errors as soon as you start fiddling with ? extends etc.
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Joshua Cranmer - 02 Jan 2008 23:45 GMT > On Tue, 01 Jan 2008 16:19:36 GMT, Joshua Cranmer > <Pidgeot18@verizon.invalid> wrote, quoted or indirectly quoted someone [quoted text clipped - 3 lines] > > What sort of changes would happen to the syntax? AFAIK, none. The proposal I am thinking of would merely allow a generic type (e.g. `T') wherever a class type is normally allowed (new T, instanceof T, (T), etc.).
> The problem now is > generics are so complicated they are more likely to introduce rather > than catch errors as soon as you start fiddling with ? extends etc. I have heard that wildcards were added very late in the generics problem so that many of the usage and style issues were not fully worked out before the release of Java 5. The moral of the story is to make sure all features are thoroughly tested before decision time comes around.
 Signature Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth
Lew - 03 Jan 2008 00:30 GMT > I have heard that wildcards were added very late in the generics problem > so that many of the usage and style issues were not fully worked out > before the release of Java 5. The moral of the story is to make sure all > features are thoroughly tested before decision time comes around. Wildcards, and more generally covariant and contravariant generic types, were in the white papers on which the whole generics scheme was built, though, and from the very beginning.
 Signature Lew
Mike Schilling - 02 Jan 2008 04:11 GMT > Why does Java have a problem with: > [quoted text clipped - 8 lines] > > Object[] things = new Object[10]; You can do
T[] things = (T[])new Object[10];
(ArrayList.java does this.) I don't mind being reminded that T[] is a lie.
Roedy Green - 02 Jan 2008 05:54 GMT On Tue, 1 Jan 2008 20:11:37 -0800, "Mike Schilling" <mscottschilling@hotmail.com> wrote, quoted or indirectly quoted someone who said :
> T[] things = (T[])new Object[10]; I still get a warning when I do that. "unchecked cast".
I guess the problem is it can't really do anything to check.
Also (String[]) new Object[10]; would always fail.
There is the complicating matter of an Object[] where all elts are strings and String[] when all elts are Strings.
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Mike Schilling - 02 Jan 2008 08:21 GMT > On Tue, 1 Jan 2008 20:11:37 -0800, "Mike Schilling" > <mscottschilling@hotmail.com> wrote, quoted or indirectly quoted [quoted text clipped - 4 lines] > I still get a warning when I do that. > "unchecked cast". Yup. No way around that. Using an array generically is inherently unsafe.
> I guess the problem is it can't really do anything to check. > > Also (String[]) new Object[10]; would always fail. > > There is the complicating matter of an Object[] where all elts are > strings and String[] when all elts are Strings. All true. What exactly is it that you're trying to do? If it's return an array of the correct element type to the caller, you'd have to do something like pass the class type in and use reflection to create the array.
Owen Jacobson - 02 Jan 2008 09:44 GMT On Jan 2, 12:21 am, "Mike Schilling" <mscottschill...@hotmail.com> wrote:
> > On Tue, 1 Jan 2008 20:11:37 -0800, "Mike Schilling" > > <mscottschill...@hotmail.com> wrote, quoted or indirectly quoted [quoted text clipped - 19 lines] > to do something like pass the class type in and use reflection to > create the array. Wild guess: Roedy's implementing a library of sort algorithms, using generics (or adapting an existing one to be "generics correct" for some value of "correct") and is trying to implement a non-in-place merge sort.
And therefore needs to be able to create holding arrays of the same element type as the input array.
Roedy: have you thought about cloning the array you're passed and then blanking it? You might also want to look at the source for ArrayList.toArray(Object[]), which uses some reflective monkeying to create an array "sufficiently large" and of the same element type as the passed array, if the passed array is not large enough to hold the list.
-o
Owen Jacobson - 02 Jan 2008 10:08 GMT > Roedy: have you thought about cloning the array you're passed and then > blanking it? You might also want to look at the source for > ArrayList.toArray(Object[]), which uses some reflective monkeying to > create an array "sufficiently large" and of the same element type as > the passed array, if the passed array is not large enough to hold the > list. Excuse the self-followup, here.
You cannot completely avoid casts, but you can use casts that are verifiably correct. Consider:
String[] args = new String[0]; // No element to query.
Class<? extends String[]> c = args.getClass (); Class<?> elt = c.getComponentType ();
We know from reading the code that elt is really String.class (which is of type Class<String>), but the depicted bounds are the most strict bounds available without casts through the Reflection API. It gets better.
Object newArr = java.lang.reflect.Array.newInstance (elt, 8);
newArr is actually a String[], which we can verify by inspection or by reflection, and you can cast the object to String[] and use it as one from there on.
The same is true if we replace String throughout with T, which is a generic type in this scope.
Part of the problem is that there is no type relationship between String[] and String that can be expressed using generics type constraints.
For completeness, here is a tested class which, given an array, creates another array of the same element type and returns it as a T[] rather than an Object[].
package com.unreasonent;
import java.lang.reflect.Array;
public class Arrays { private Arrays () { assert (false); }
public static <T> Class<?> getElementClass (T[] array) { return array.getClass ().getComponentType (); }
@SuppressWarnings("unchecked") public static <T> T[] createArrayLike (T[] original, int length) { Class<?> eltType = getElementClass (original); return (T[]) Array.newInstance (eltType, length); } }
-o
Roedy Green - 06 Jan 2008 10:04 GMT On Wed, 2 Jan 2008 01:44:48 -0800 (PST), Owen Jacobson <angrybaldguy@gmail.com> wrote, quoted or indirectly quoted someone who said :
>Wild guess: Roedy's implementing a library of sort algorithms, using >generics (or adapting an existing one to be "generics correct" for [quoted text clipped - 3 lines] >And therefore needs to be able to create holding arrays of the same >element type as the input array. You got it. You can see how well I have done http://mindprod.com/products.html#SHELLSORT http://mindprod.com/products.html#RADIXSORT http://mindprod.com/products.html#HEAPSORT http://mindprod.com/products.html#QUICKSORT
The code WORKS fine, it just bugs me to have a warning. I could use annotations to suppress it, but that is cheating. It seems to there should be a way to do this cleanly.
When I was a kid I used to sell mimeographed cheat sheets for five cents each to my fellow students. I created mindless algorithms to solve various sorts of problems. The teachers would plug them for me. I composed them for math, physics, chemistry, genetics...
I would, as an adult, like to cook one up for generics. But first I have to properly understand it myself. It is still pretty foggy, despite quite a bit of reading.
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Lew - 06 Jan 2008 17:32 GMT > I would, as an adult, like to cook one up for generics. But first I > have to properly understand it myself. It is still pretty foggy, > despite quite a bit of reading. The fault, dear Brutus, lies not in our stars but in our Sun - that is, the Java language monarchs, when they implemented generics as compile-time only. This forced compromises in the mental model of generics. This also causes "reification of generics" to be a very hot topic in Java.
If you regard "unchecked" warnings as manifestations of the core hack, you might find your mental model easier to build.
There are games you can play with things like a Class <SomeType> as a run-time-type marker to ease the pain of generics. Elimination of unchecked warnings is often (not, I think still, always) possible with a rococo assemblage of Class<? extends Whatever> and holder classes with generic methods.
 Signature Lew
Mike Schilling - 06 Jan 2008 18:43 GMT > On Wed, 2 Jan 2008 01:44:48 -0800 (PST), Owen Jacobson > <angrybaldguy@gmail.com> wrote, quoted or indirectly quoted someone [quoted text clipped - 18 lines] > annotations to suppress it, but that is cheating. It seems to there > should be a way to do this cleanly. But there isn't. And if it's any consolation to you, java/util/ArrayList.java compiles with a warning too (or perhaps these days it uses the annotation to remove it.)
Roedy Green - 06 Jan 2008 10:06 GMT On Wed, 2 Jan 2008 01:44:48 -0800 (PST), Owen Jacobson <angrybaldguy@gmail.com> wrote, quoted or indirectly quoted someone who said :
>Roedy: have you thought about cloning the array you're passed and then >blanking it? I thought of that, but have not yet tried it. I have many messy little projects on my plate all half done.
It smells a bit too strongly of kludge. I can get the object that way. But can I get the reference?
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Roedy Green - 06 Jan 2008 09:59 GMT On Wed, 02 Jan 2008 08:21:41 GMT, "Mike Schilling" <mscottschilling@hotmail.com> wrote, quoted or indirectly quoted someone who said :
>What exactly is it that you're trying to do? I am doing a sort and I need an aux array of pointers to the things to sort. Ideally it would be declared as String[] when I was sorting String and Orange[] when I was sorting Oranges.
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|