Java Forum / General / November 2005
stupid generics
zero - 04 Nov 2005 16:43 GMT When I first heard about generics I was excited. A great idea finally available in Java. But the way they are implemented really makes no sense. Because of type erasure generics do nothing that 1.4 code didn't already do by using Object as "generic" class for all (sub)classes.
And the problem with arrays of generic classes? Simply ridiculous. There is no sane reason why the following code (from a self-learning chess program I've been working on) should not work, and yet...
ArrayList<Piece>[] board; board = new ArrayList<Piece>[8]; // error: cannot create a generic // array of ArrayList<Piece> board[0] = new ArrayList<Piece>(); board[0].add(0, new Rook(false, 0, 8));
where of course Rook is a subclass of Piece.
I'm forced to use board = new ArrayList[8]; which of course gives an unchecked warning.
They should just throw away the whole generics thing and replace it with a C++ like approach.
Daniel Dyer - 04 Nov 2005 16:59 GMT > And the problem with arrays of generic classes? Simply ridiculous. > There [quoted text clipped - 11 lines] > I'm forced to use board = new ArrayList[8]; which of course gives an > unchecked warning. I share your frustration with the difficulty in creating a generic array (it is possible with the java.lang.reflect.Array.newInstance method and an unchecked cast), but why not model the board as a two dimensional array of pieces?
Piece[][] board = new Piece[8][8];
Dan.
 Signature Daniel Dyer http://www.dandyer.co.uk
zero - 04 Nov 2005 17:15 GMT > I share your frustration with the difficulty in creating a generic > array (it is possible with the java.lang.reflect.Array.newInstance [quoted text clipped - 4 lines] > > Dan. To be honest I don't remember why I chose the array of arraylists. I wrote this code over a year ago, but then other things got in the way and the project was postponed. I just started cleaning up the code to move to 1.5 and continue development.
I guess a board doesn't change size, so there's no real point in using arraylists. I may indeed change it, thanks for pointing it out.
Doesn't change the fact that generics are stupid though ;-)
Daniel Dyer - 04 Nov 2005 17:18 GMT > it is possible with the java.lang.reflect.Array.newInstance method and > an unchecked cast I don't know why I made that so complicated, it's just equivalent to this:
ArrayList<Piece>[] board = (ArrayList<Piece>[]) new ArrayList[8];
You will get a warning (which you can suppress with the @SuppressWarnings annotation, or at least you will be able to once it is implemented properly in 1.6), but at leat you will get compile-time type checking on subsequent operations involving the generic list.
Dan.
 Signature Daniel Dyer http://www.dandyer.co.uk
Roedy Green - 05 Nov 2005 10:52 GMT >board = new ArrayList<Piece>[8]; // error: cannot create a generic > // array of ArrayList<Piece> ArrayList uses () not [] to define the size.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 05 Nov 2005 10:55 GMT > board = new ArrayList[8]; this means you want 8 ArrayLists, not an ArrayList of 8 elements. Is that what you meant?
If you did want 8 ArrayLists, you still have initialise each of the 8 slots with an ArrayList object.
See http://mindprod.com/jgloss/gotchas.html#ARRAY
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
zero - 05 Nov 2005 17:02 GMT >> board = new ArrayList[8]; > this means you want 8 ArrayLists, not an ArrayList of 8 elements. Is [quoted text clipped - 4 lines] > > See http://mindprod.com/jgloss/gotchas.html#ARRAY Hence the next line in my code: board[0] = new ArrayList<Piece>();
Obviously this is part of a for loop, I just slimmed down the code. That was not really the point of my post anyway. The point was, it is *impossible* to create an array of parameterized types without getting a warning.
Roedy Green - 05 Nov 2005 11:00 GMT >board = new ArrayList<Piece>[8]; // error: cannot create a generic For a chessboard, you have precisely 8 row and 8 columns. That will never change. Therefore you should use an array not an ArrayList of ArrayLists. It is much simpler code.
Piece[][] board = new Piece[8][8]; for ( int row=0; row<8; row++ ) { for ( int col=0; col<8; col++ ) { board[row][col] = new Piece(); } }
You don't need generics with arrays. Typing in built in properly.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
zero - 05 Nov 2005 17:03 GMT >>board = new ArrayList<Piece>[8]; // error: cannot create a generic > [quoted text clipped - 13 lines] > > You don't need generics with arrays. Typing in built in properly. Yes, Dan pointed that out already too. See my reply to his post.
Thomas Hawtin - 05 Nov 2005 20:54 GMT >>board = new ArrayList<Piece>[8]; // error: cannot create a generic > > For a chessboard, you have precisely 8 row and 8 columns. That will > never change. Therefore you should use an array not an ArrayList of > ArrayLists. It is much simpler code. Might want to use the code for Go boards as well. Perhaps.
I would tend to make the Board a class itself, and slightly optimise/get rid of the array of arrays confusion. (A List would do in place of the array.)
private int index(int row, int col) { if (!( 0 <= row && row < size && 0 <= col && col < size )) { throw new IllegalArgumentException(); } return row*size + col; } public Piece get(int row, int col) { return board[index(row, col)]; }
> You don't need generics with arrays. Typing in built in properly. Generics is a built in property of arrays. You don't need arrays of generics.
Tom Hawtin
 Signature Unemployed English Java programmer http://jroller.com/page/tackline/
Googmeister - 05 Nov 2005 21:11 GMT > Generics is a built in property of arrays. You don't need arrays of > generics. I'm not sure I follow. I agree the OPs example was poor motivation, but many of us would very much like to be able to create arrays of generics, e.g., array implementation of a stack.
Roedy Green - 06 Nov 2005 07:45 GMT >> Generics is a built in property of arrays. You don't need arrays of >> generics. > >I'm not sure I follow. With ArrayList you use <Thing> notation to tell the compiler what sort of beasts will live in the ArrayList. With plain arrays, you just use Thing[] without that notation. You sidestep all the quirky generics issues of type erasure etc. With arrays, the typing is built in properly rather than hacked with generics.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Googmeister - 06 Nov 2005 11:17 GMT > >> Generics is a built in property of arrays. You don't need arrays of > >> generics. [quoted text clipped - 6 lines] > issues of type erasure etc. With arrays, the typing is built in > properly rather than hacked with generics. OK, I see. To me "array of generics" refers to what you want to do when you implement an ArrayList (or any other array-based data structure).
public class ArrayList<Item> { private Item[] elements; // error: "can't create an array of generics" ...
I'd very much like to do this. Generics are particularly useful with collections and other data structures, but the language gets in your way if you try to create your own.
Roedy Green - 06 Nov 2005 12:07 GMT >public class ArrayList<Item> { > private Item[] elements; // error: "can't create an array of >generics" I wrote a little program to confirm this to myself:
import java.util.ArrayList; /** * explore use of Generics and arrays */ public class GenAr
{ /** * test harness * * @param args not used */ public static void main ( String[] args ) { ArrayList<String>[] stuff = new ArrayList<String>[ 10 ]; } }
I feel embarrassed that Java's generics are so Mickey Mouse they don't even integrate with arrays.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
zero - 06 Nov 2005 12:57 GMT > I feel embarrassed that Java's generics are so Mickey Mouse they don't > even integrate with arrays. That almost sounds like an insult to Mickey Mouse ;-)
Thomas G. Marshall - 06 Nov 2005 18:02 GMT Roedy Green coughed up:
...[rip]...
> public static void main ( String[] args ) > { [quoted text clipped - 4 lines] > I feel embarrassed that Java's generics are so Mickey Mouse they don't > even integrate with arrays. Yes. And I feel equally embarrassed, but am holding off most of my condemnation of it until I get a better handle on how badly things would have been had they added a runtime generic type.
Please refer to my new thread in comp.lang.java.advocacy:
Subject: runtime generics: Could JVM's solve the backward incompatibility?
 Signature Doesn't /anyone/ know where I can find a credit card company that emails me the minute something is charged to my account?
Tor Iver Wilhelmsen - 06 Nov 2005 18:51 GMT > Yes. And I feel equally embarrassed, but am holding off most of my > condemnation of it until I get a better handle on how badly things > would have been had they added a runtime generic type. Are you referring to Smalltalk's message-based structure and #doesNotUnderstand:aMessage if a given object does not understand an attempted method invocation? That would be keen, yes. But then, having Smalltalk win the language wars would have been nice, but thanks to expensive licenses and infighting it was not to be.
(I flatly refuse to read clj.advocacy, you see. :) )
Thomas G. Marshall - 06 Nov 2005 05:07 GMT Thomas Hawtin coughed up:
...[rip]...
>> You don't need generics with arrays. Typing in built in properly. > > Generics is a built in property of arrays. You don't need arrays of > generics. That doesn't make any sense to me at all. In this example:
ArrayList<Apple>[] allGroves = new ArrayList<Apple>[100];
I would be asking for an array of ArrayLists's that each only take Apples. This would prevent someone from doing this by accident later:
allGroves[50] = new ArrayList<Orange>();
Regardless of whether or not the OP has modeled his particular issue correctly, not having this ability seems an annoyance that /might/ not have been necessary.
 Signature With knowledge comes sorrow.
Thomas Hawtin - 06 Nov 2005 06:01 GMT > Thomas Hawtin coughed up: > [quoted text clipped - 17 lines] > correctly, not having this ability seems an annoyance that /might/ not have > been necessary. What I was trying to say (extremely badly) is that arrays fit in with the concept of genericity in that they have the property of being generic. Unfortunately they do it with different (and less safe rules) than the rest of the language. Once we have something the wraps arrays, then the only real need for arrays of references is for optimisation. Mixing the two is just an annoyance.
Tom Hawtin
 Signature Unemployed English Java programmer http://jroller.com/page/tackline/
Thomas G. Marshall - 06 Nov 2005 17:43 GMT Thomas Hawtin coughed up:
>> Thomas Hawtin coughed up: >> [quoted text clipped - 24 lines] > generic. Unfortunately they do it with different (and less safe rules) > than the rest of the language. Arrays are objects that maintain their own type contents, which is generic behavior, sure. But in what way is this less safe than the rest of the language? It is certainly far safer than generics in general in that arrays exhibit this behavior at runtime as well.
> Once we have something the wraps arrays, > then the only real need for arrays of references is for optimisation. Sure, but he's not wrapping the array. He's attempting to have an array of a type of a type.
...[rip]...
 Signature Doesn't /anyone/ know where I can find a credit card company that emails me the minute something is charged to my account?
Googmeister - 06 Nov 2005 17:54 GMT > Thomas Hawtin coughed up: > >> Thomas Hawtin coughed up: [quoted text clipped - 29 lines] > behavior, sure. But in what way is this less safe than the rest of the > language? One unsavory consequence of covariant arrays in Java is the ArrayStoreException.
Object[] a = new String[10]; // OK a[0] = "hello"; // OK a[1] = new Integer(42); // a runtime error
Thomas G. Marshall - 06 Nov 2005 18:05 GMT Googmeister coughed up:
>> Thomas Hawtin coughed up: ...[rip]...
>>> What I was trying to say (extremely badly) is that arrays fit in with >>> the concept of genericity in that they have the property of being [quoted text clipped - 12 lines] > a[0] = "hello"; // OK > a[1] = new Integer(42); // a runtime error That's precisely the protection you /want/ at runtime. However, I believe that your complaint with this is that it isn't something caught at compile-time?
 Signature Doesn't /anyone/ know where I can find a credit card company that emails me the minute something is charged to my account?
Googmeister - 06 Nov 2005 20:30 GMT > Googmeister coughed up: > >> Thomas Hawtin coughed up: [quoted text clipped - 21 lines] > that your complaint with this is that it isn't something caught at > compile-time? Yes, my complaint is that you have to wait until runtime to notice such things. If arrays weren't covariant, Java could detect such errors at compile time.
Thomas G. Marshall - 06 Nov 2005 20:38 GMT Googmeister coughed up:
>> Googmeister coughed up: >>>> Thomas Hawtin coughed up: [quoted text clipped - 26 lines] > such things. If arrays weren't covariant, Java could detect such > errors at compile time. ok.
 Signature Enough is enough. It is /not/ a requirement that someone must google relentlessly for an answer before posting in usenet. Newsgroups are for discussions. Discussions do /not/ necessitate prior research. If you are bothered by someone asking a question without taking time to look something up, simply do not respond.
Brendan Guild - 09 Nov 2005 09:39 GMT >> Googmeister coughed up: >> > One unsavory consequence of covariant arrays in Java is the [quoted text clipped - 11 lines] > such things. If arrays weren't covariant, Java could detect such > errors at compile time. But at the cost of greatly reducing the power of arrays and restricting a perfectly reasonable operation on arrays. It would be just silly to forbid us from performing operations on abstract arrays without declaring the type of the array.
With covariant arrays I can write a procedure to reverse the order of the elements and I won't have to rewrite it for every class that I might have an array for. Functional languages devote lots of syntax to what they call 'polymorphism' just to get this kind of power. For them it feels special; for us it comes naturally, so long as we don't fight it.
The reason that ArrayStoreExceptions are so rare is that the programming error they represent is so obvious and natural that people almost never make it.
Googmeister - 09 Nov 2005 12:27 GMT > >> Googmeister coughed up: > >> > One unsavory consequence of covariant arrays in Java is the [quoted text clipped - 20 lines] > the elements and I won't have to rewrite it for every class that I might > have an array for. Sure, I assume that was the original motivation for covariant arrays (as I indicated earlier with an Arrays.sort example), and now Java is stuck with it. But if you were designing a type system from scratch with generics, why not let generics be the way to accomplish the generic array behavior you describe? Why not make List<Item> and Item[] behave more like one another, rather than less?
>The reason that ArrayStoreExceptions are so rare is that the programming > error they represent is so obvious and natural that people almost never > make it. I have to agree that I've never encountered one myself. But I know that every time I assign something to an array, it is (needlessly) checked at run-time, hopefully by an optimizing compiler.
Thomas Hawtin - 06 Nov 2005 20:59 GMT > Thomas Hawtin coughed up: >> [quoted text clipped - 7 lines] > language? It is certainly far safer than generics in general in that arrays > exhibit this behavior at runtime as well. A program that does not generate mandatory warnings should only generate ClassCastExceptions at explicit casts. I think that is significant for a strong, statically typed language. Without that we might as well all use Smalltalk.
Warning-free, castless programs using arrays of references can generate runtime exceptions in quite nasty ways.
> Sure, but he's not wrapping the array. He's attempting to have an array of > a type of a type. Quite. He shouldn't be. Dumping now unnecessary uses of arrays is part of upgrading to generics.
Tom Hawtin
 Signature Unemployed English Java programmer http://jroller.com/page/tackline/
Thomas G. Marshall - 06 Nov 2005 22:18 GMT Thomas Hawtin coughed up:
>> Thomas Hawtin coughed up: >>> [quoted text clipped - 24 lines] > Quite. He shouldn't be. Dumping now unnecessary uses of arrays is part > of upgrading to generics. That's still the rub. In /what/ way is what he is attempting an example of an unnecessary use of arrays?
As I said elsewhere, he wants something akin to this. An array of 100 apple groves, each grove an arraylist of apples.
ArrayList<Apple>[] allGroves = new ArrayList<Apple>[100];
to prevent someone from doing this by accident later:
allGroves[50] = new ArrayList<Orange>();
Why should he want to avoid this?
 Signature I've seen this a few times--Don't make this mistake:
Dwight: "This thing is wildly available." Smedly: "Did you mean wildly, or /widely/ ?" Dwight: "Both!", said while nodding emphatically.
Dwight was exposed to have made a grammatical error and tries to cover it up by thinking fast. This is so painfully obvious that he only succeeds in looking worse.
Thomas Hawtin - 07 Nov 2005 00:25 GMT > That's still the rub. In /what/ way is what he is attempting an example of > an unnecessary use of arrays? An array is unnecessary because List would have done instead. It's quite unnecessary to drop down to a less able type. Reference arrays bad.
> As I said elsewhere, he wants something akin to this. An array of 100 apple > groves, each grove an arraylist of apples. > > ArrayList<Apple>[] allGroves = new ArrayList<Apple>[100]; That should normally be written:
List<List<Apple>> allGroves = new ArrayList<List<Apple>>(100);
Or better:
List<Grove> allGroves = new ArrayList<Grove>(100);
That also has the advantage that there are no uninitialised elements/components hanging around.
Tom Hawtin
 Signature Unemployed English Java programmer http://jroller.com/page/tackline/
Thomas G. Marshall - 07 Nov 2005 01:14 GMT Thomas Hawtin coughed up:
>> That's still the rub. In /what/ way is what he is attempting an example >> of [quoted text clipped - 19 lines] > That also has the advantage that there are no uninitialised > elements/components hanging around. I disagree with your overall notion. This is just a statement that he could replace arrays with arraylists, which should not be his only recourse here.
 Signature It's time for everyone to just step back, take a deep breath, relax, and stop throwing hissy fits over crossposting.
zero - 06 Nov 2005 12:30 GMT > Thomas Hawtin coughed up: > [quoted text clipped - 17 lines] > correctly, not having this ability seems an annoyance that /might/ not > have been necessary. The generics tutorial(1) gives a reason for this annoyance, however I think it could all have been sidestepped by a better implementation for generics.
<disclaimer> Following code is NOT Java, but is the way I wish it would work. (based on the way templates are implemented in C++) </disclaimer>
Take generic class GenericContainer, with type parameter T:
public class GenericContainer<T> { T[] data;
public GenericContainer(int size) { data = new T[size]; }
public T get(int index) { return data[index]; }
// ... }
When you use this class, for example;
GenericContainer<MyClass> c = new GenericContainer<MyClass>(5);
the compiler should generate a GenericContainerOfMyClass class, which would be:
public class GenericContainerOfMyClass { MyClass[] data;
public GenericContainerOfMyClass(int size) { data = new MyClass[size]; }
public MyClass get(int index) { return data[index]; }
// ... }
Thus a different class would be created for each type of GenericContainer you want.
This would greatly improve things. The way generics are implemented now I see only 2, very minor, advantages over 1.4 code: the compiler checks the type, and you don't have to typecast when for example getting an object from a collection.
(1) http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
Ross Bamford - 06 Nov 2005 14:29 GMT >> ...[rip]... >> [quoted text clipped - 17 lines] > [ ... ] > (1) http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf Hmm, that's interesting. In that, they give the reason for avoiding this kind of thing as
"This restriction is necessary to avoid situations like:
List<String>[] lsa = new List<String>[10]; // not really allowed Object o = lsa; Object[] oa = (Object[]) o; List<Integer> li = new ArrayList<Integer>(); li.add(new Integer(3)); oa[1] = li; // unsound, but passes run time store check String s = lsa[1].get(0); // run-time error - ClassCastException
If arrays of parameterized type were allowed, the example above would compile without any unchecked warnings, and yet fail at run-time."
Perhaps it's just me, but I don't really see much difference between that and:
List<String> lsa = new ArrayList<String>(); // ... but this is. Object o = lsa; Object[] oa = (Object[]) o; List<Integer> li = new ArrayList<Integer>(); li.add(new Integer(3)); oa[1] = li; // still unsound, and *no* runtime store check String s = lsa.get(0); // run-time error - ClassCastException
Have I missed something, or is it just a pretty poor excuse. If the compiler couldn't check array access at all then I could understand the reasoning, but it can - I just can't see any real barriers that prevent generic array instantiation. What am I missing?
 Signature Ross Bamford - rosco@roscopeco.remove.co.uk
Googmeister - 06 Nov 2005 16:46 GMT > > The generics tutorial(1) gives a reason for this annoyance, however I > > think it > > could all have been sidestepped by a better implementation for generics. My understanding is that the underlying reason is that arrays are "covariant" but generics are not. In other words, String[] is a subtype of Comparable[], but List<String> is not a subtype of List<Comparable>.
In a world without generics, covariant arrays are useful, e.g., to implement Arrays.sort(Comparable[]) and have it be callable with an input of type String[]. But now that Java has generics, the usefulness of covariant arrays is diminished, but we are stuck with them.
Thomas Hawtin - 06 Nov 2005 20:59 GMT >> (1) http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf > [quoted text clipped - 6 lines] > Object o = lsa; > Object[] oa = (Object[]) o; This cast is unnecessary, as things stand.
> List<Integer> li = new ArrayList<Integer>(); > li.add(new Integer(3)); [quoted text clipped - 10 lines] > Object o = lsa; > Object[] oa = (Object[]) o; You think this is going to work?
> List<Integer> li = new ArrayList<Integer>(); > li.add(new Integer(3)); > oa[1] = li; // still unsound, and *no* runtime store check > String s = lsa.get(0); // run-time error - ClassCastException > > Have I missed something, or is it just a pretty poor excuse. If the You've missed something.
Tom Hawtin
 Signature Unemployed English Java programmer http://jroller.com/page/tackline/
Ross Bamford - 07 Nov 2005 09:18 GMT >>> (1) http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf >>> [quoted text clipped - 6 lines] > > This cast is unnecessary, as things stand. I know, but this is Sun's code. It's 'the reason they didn't implement generic arrays', according to the PDF link Zero posted. I just quoted it as context for:
>> List<Integer> li = new ArrayList<Integer>(); >> li.add(new Integer(3)); [quoted text clipped - 10 lines] > > You think this is going to work? No. I was saying that, I didn't see that much difference - the second example (which is just a slightly modified version of the original) compiles cleanly with no unchecked warning, but throws CCE at runtime. Therefore, the reason given for avoiding generic array instantiation is pretty weak, given that it can happen easily anyway, no? Surely the convenience would have outweighed the risks?
>> List<Integer> li = new ArrayList<Integer>(); >> li.add(new Integer(3)); [quoted text clipped - 6 lines] > > Tom Hawtin
 Signature Ross Bamford - rosco@roscopeco.remove.co.uk
Thomas Hawtin - 07 Nov 2005 15:21 GMT >>> List<String>[] lsa = new List<String>[10]; // not really allowed >>> Object o = lsa; >>> Object[] oa = (Object[]) o;
>>> Perhaps it's just me, but I don't really see much difference >>> between that and: [quoted text clipped - 7 lines] > example (which is just a slightly modified version of the original) > compiles cleanly with no unchecked warning, but throws CCE at runtime. Not much difference?? In the first example an array is being cast to an array (the particular example can be done without the explicit cast). The second is casting from an *array* to a *List*. Well, duh.
> Therefore, the reason given for avoiding generic array instantiation is > pretty weak, given that it can happen easily anyway, no? Drivel.
> Surely the > convenience would have outweighed the risks? My idea of convenience includes compatible semantics.
Tom Hawtin
 Signature Unemployed English Java programmer http://jroller.com/page/tackline/
Ross Bamford - 06 Nov 2005 15:23 GMT > <disclaimer> > Following code is NOT Java, but is the way I wish it would work. (based [quoted text clipped - 48 lines] > GenericContainer you > want. I think the current system achieves a reasonable balance between flexibility on the one hand, and performance and backward compatibility on the other. I'm not saying it's perfect (*far* from it), but I think that if they had gone the bytecode-generation route it would have been a lot less useful - an unrestricted number of new classes being generated on the fly, entirely 'under the hood' (i.e. outside your app's control). It might make even basic operations entirely unpredictable in terms of time, compromise security, and make it much more difficult to implement custom classloading strategies. I expect it's likely that the generics team started with something like above, given that bytecode generation is used elsewhere in the platform, and then worked backward to try to make it performant and compatible.
> This would greatly improve things. The way generics are implemented now > I see > only 2, very minor, advantages over 1.4 code: the compiler checks the > type, and > you don't have to typecast when for example getting an object from a > collection. I think generics are a great convenience for working with collections, and useful in other situations too. I think the problem is more that they promise too much - it's natural to assume you can do things like T.newInstance() and new List<String>[40]. Erasure is certainly a minefield of ifs and elses, but I'm not convinced that creating separate classes under the hood is a viable alternative.
 Signature Ross Bamford - rosco@roscopeco.remove.co.uk
Thomas Hawtin - 06 Nov 2005 20:59 GMT > I think the current system achieves a reasonable balance between > flexibility on the one hand, and performance and backward compatibility [quoted text clipped - 8 lines] > given that bytecode generation is used elsewhere in the platform, and > then worked backward to try to make it performant and compatible. C++-style code generation would have been nuts. (C++ templates can share object code, but most compilers do not do so.)
The alternative I prefer would insert a layer of indirection between objects and classes. Most problems in computer science can be resolved by an additional layer of indirection.
Instead of each object having a pointer to the details of its runtime implementation class, it would have a pointer to a structure containing a pointer to the old implementing class details and its runtime generic types. Code would be shared, as before, and runtime casts at point of use. (You could imagine type-specific optimisation variants on this theme.)
Old code implementing new types would simply have blanks for the generic information. When it comes to casting generic types, anything blank would just pass, to be checked at use. But for new implementations, types can be checked and used for reflection based activities.
Tom Hawtin
 Signature Unemployed English Java programmer http://jroller.com/page/tackline/
Tor Iver Wilhelmsen - 06 Nov 2005 18:26 GMT > When you use this class, for example; > > GenericContainer<MyClass> c = new GenericContainer<MyClass>(5); The problem with C++'s approach is that you don't use that class, you use that *template*. There is no base GenericContainer class to cast to, since there is no common base class Object.
It's quite easy to make a template system of your own, e.g. using Ant to generate the concrete classes, if you find that to be sufficient.
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 ...
|
|
|