I've been reading over the Generics tutorial and have noticed that they seem to impare the ability to add new unknown-types objects to a list of unknown-but-similarily-typed objects.
How would I do this? An example of what I am doing in 1.4 is below:
Basically, I am taking a list of objects of unknown type (List<?> in generics) and, using reflection, creating a new object that I then add to the list. However, as per the tutorial, you can't add objects into a List<?>; you can only get from.
These objects are not able to be subclassified any further than Objects (other than they have to have a <T>() constructor for the reflection-based instantiation to work....) They could be customer obejcts or java.io.File objects or anything else.
Since Generics is enforced by the compiler, I suppose that I could use reflection to get around this, but that seems a bit like cheating. Is there some way to do this unsing generics?
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
It's all part of the type-safety argument isn't it? It forces you to make your intent explicit, i.e. that your List can take any Object. The main issue that I see with generics in Beta 2 is that you can't get around the warnings for legacy Collections code. If the final release includes a compiler switch to allow for that (specifically enough) then there's effectively no difference.
This dicussion thread points out what I have been saying about how generics changes the Java langauge. Generics also change how we think about the programming solutions that we implement. Some techniques, such as a list of unknown objects, are still possible, but the addition of generics points us away from thinking in these terms. Generics encourage us to think in terms of a specific type, or set of types related by a common superclass (other than Object, of course).
I think that it will take the Java community a year or more to fully come to terms with the implications of generics.
Java is changing, and we will have to change with it.
For my latest books on Java, including my Java Programming Cookbook, see HerbSchildt.com
Joined: Mar 04, 2004
Generics also change how we think about the programming solutions that we implement. Some techniques, such as a list of unknown objects, are still possible, but the addition of generics points us away from thinking in these terms.
I'm seeing some stuff between the lines, here - but I'm wondering if what I'm seeing is really there.
This could be read as saying that there's a fundamental shift away from Smalltalk style late binding and weak(er) typing, and towards C++ style early binding and strong(er) typing. Put another way, it would be a shift away from an extremely object oriented paradigm, and toward classes as primarily a way of managing code.
Originally posted by Warren Dew: This could be read as saying that there's a fundamental shift away from Smalltalk style late binding and weak(er) typing, and towards C++ style early binding and strong(er) typing.
I think you mixed terms here. Smalltalk is strongly and dynamically typed, C++ weakly and statically.
Put another way, it would be a shift away from an extremely object oriented paradigm, and toward classes as primarily a way of managing code.
I don't think this is true at all. The most important feature of OO languages is polymorphism, and Tiger isn't changing that. On the other hand I would say that polymorphism *is* a tool for managing code (that is, code dependencies).
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus