Hi,
I've started migrating the codebase to Java 5, as there are several
important benefits to getting this done as soon as possible. The
primary benefit has to be generics, but covariant return types(for
tree structures such as Syntax, Expr and Action) and varargs will come
in extremely useful.
The most important note so far is that it appears impossible to create
a typed array, e.g.
class Example< T > {
T[] store;
public Example() {
this.store =3D new T[ 10 ]; // This is illegal
this.store =3D (T[]) new Object[ 10 ]; // and this issues a warning
}
public Example( T[] elements ) {
this.store =3D elements; // This is ok
}
}
I don't see a practical way around this, as the methods for creating
arrays dynamically(java.lang.reflect.Array#newInstance) return Object
arrays not typed arrays. The main issue that arises from this is
compiling the code will produce warnings and disabling those warnings
may hide other important potential issues.
The most productive description of this problem I've found is the following=
:
http://www.langer.camelot.de/GenericsFAQ/FAQSections/ParameterizedTypes.htm=
l#Can%20I%20create%20an%20array%20whose%20component%20type%20is%20a%20concr=
ete%20instantiation%20of%20a%20parameterized%20type%3F
but their three suggested solutions are not of any real help, e.g.
1) use a raw type - ok we could use Object, but then we would have to
cast every use of the array to type T which generates a warning.
2) use a wildcard - ok, but essentially the same as (1)
3) use a Collection - not the most helpful of suggestions if you are
deliberately writing this kind of code
It seems that grudgingly I'll be forced to suppress the warning that
causes this.
Kev.
|