Thomas G. Marshall said:
Interesting. I see generics as a step BACKWARDS with regard to
"self-documentation".
In the realm of C++, I've always considered one of the DISADVANTAGES of
templates as being to provide a way to AVOID having to document an
interface.
Let me explain.
Correct me if I'm wrong, but... in C++ templates classes can implement
PRESUMPTIONS about the parameterized types. That is, I can write a
template class that ASSUMES that one of the parameterized class-types I pass
in provides a method called "m".
This is commonly used in C++ to avoid the confusion, performance penalties
and overall ugliness of multiple-inheritance.
Now I haven't used generics in Java (I'm leaning heavily towards FORBIDDING
their use in all but the container classes but...
Isn't it true that I can write generics in Java that make presumptions about
the type that have absolutely ZERO "documentation" other than any comments
the implementer is gracious enough to provide? ISn't it true that even
Javadoc will NOT give ANY indication of this vital piece of information?
That is, can't I (in Java as in C++) write a generic class that presumes the
class type passed in implements a particular combination of methods and
fields? And is it not true that there's no way in the language (other than
comments) to say that "this type parameter must be assignment-compatible
with a particular class or interface?
So... unless I'm missing something about generics, it strikes me that while
it does provide a mildly useful piece of documentation about types, it also
opens the door for writing code that is far more likely to create a more
serious documentation gap than the one it "fixes"?
db
p.s. I must say that thus far, I personally have seen no remotely
compelling arguments for generics in Java. But that's just my opinion.
To me templates in C++ were a mistake created to fix another of uncle
Bjarne's mistakes: not providing a common root for class types. A classic
C++ blunder on top of blunder.
One of the things I like about Java is that unlike C++, it is very
well-designed and avoided the vast amount of mistakes made in C++. It's a
bit disturbing to see a mistake in C++ start creeping in.
I've been told that the main reason that C++'s STL hasn't caught on is that
its so difficult to avoid the pratfalls set up by its use of templates (code
bloat, etc.... all those things mentioned in Meyer's "Defective C++" book).
(FYI: Sun's HotSpot is essential STL-free.)
Compare the adoption of STL with that of Java's JRE. No one thinks twice
about using the JRE.