I have the following code, which I'm using to read each element from an array in turn. I've recently modified it to work with an array of any type, but it no longer works.
When I use it like this, I am told that the constructor ArrayReader<Character>(char) is undefined.
What's going on here? Thanks v much for any help....
Kind regards, John [ March 28, 2006: Message edited by: John Wickerson ]
Joined: Sep 16, 2005
You are bumping up against a basic limitation of generic types in Java: T doesn't range over primitive types like char. To make your example work, you have to copy your char into a Character. I don't know any method to do that, so just roll up your sleeves and write a loop.
There is no emoticon for what I am feeling!
Joined: Sep 24, 2003
Originally posted by Jeff Albertson: You are bumping up against a basic limitation of generic types in Java: T doesn't range over primitive types like char. To make your example work, you have to copy your char into a Character. I don't know any method to do that, so just roll up your sleeves and write a loop.
Indeed, this is a horrible limitation, however, generics are not the cause. The existence of both primitive types and arrays is the cause, and as a consequence, generics are unable to achieve this common use case due to the leak of abstraction.
ContractualJ attempts to work around the whole mess of primitives with the net.tmorris.primitives package and the mess of arrays with net.tmorris.sequence. The solution is partially optimal, but as optimal as possible within the constraints of the assumed context (e.g. must compile with a Java compiler is a devastational constraint but one that I choose to impose).
I continually find it astonishing that so many subscribe to the legitimacy of these constructs, albeit suffering consequences of this blind faith often stated at the same time!! ...quite often attribution of the cause to a consequence of the cause, instead of externalising the issue for analysis and deriving the real cause (which sometimes contradicts the belief of the establishment).
Thanks Jeff and Tony. I must admit it's quite a satisfying feeling when I finds that my question does not have a blindingly obvious answer that I should have seen. Since I'm only using the ArrayReader with chars at the moment, I guess I'll fix it to become a CharArrayReader instead.