Re: Educate me on generics please...
Re: Educate me on generics please...
- Subject: Re: Educate me on generics please...
- From: David Elliott <email@hidden>
- Date: Fri, 8 Feb 2008 02:25:42 -0500
On Feb 7, 2008, at 7:45 PM, Ian Joyner wrote:
On 07/02/2008, at 3:41 PM, Chuck Hill wrote:
I agree. Generics as a concept are useful. It is just that most
developer's contact with them has been either C++ or Java. Neither
is a shining example.
C++ templates are just horrible leading to code bloat, but since it
was hacked on later it had to duplicate all code for different
generic parameters to preserve type info at run time. Java went
completely the other way, avoiding bloat, but losing run time info.
Mind you, it probably isn't so important in Java since everything is
an object, but in C++ you can have objects and primitive types as
generic parameters (or can you do that in Java?).
I've got to chime in here and defend C++ a bit. Templates are not
generics. Templates are templates. The very name of them should
leave one with the impression that they are a template for generating
real code and are not in and of themselves real code. There is no
such thing as a "template class" or a "template function" in C++.
There are class templates and function templates. That is, the word
"template" is the object (i.e. the noun) not the modifier (i.e. the
adjective) when you speak of templates in C++. It is unfortunate that
the vast majority of C++ literature fails to accurately explain what
templates really are.
Once you understand that you come to the realization that in order to
implement what most people think of as generics you typically want to
put the meat of the implementation in a class and only the casting
ugliness in a class template. When you do it this way you completely
avoid all of the supposed drawbacks of templates because you
essentially wind up implementing them in much the same way that Java
implements generics.
The bottom line is that the template system is really a glorified
macro preprocessor.
I am still not quite convinced about multiple inheritance, but I
blame that on C++. :-)
I just find the whole Class/Interface separation rather artificial.
No one has really put up a convincing argument for Java's so-called
"theoretical correctness" on this matter. It's the old interface
inheritance vs. implementation inheritance debate. Without multiple
implementation inheritance, Java has inner classes, etc to
supposedly get around this, but that makes my head hurt just as much
as the details of their generics.
One convincing argument is that in C++ you are much better off
separating your multiple inheritance into two distinct types. The
first is where you inherit a class with only pure-virtual functions.
Doing it this way you wind up with what amounts to exactly the same
thing as a Java interface.
The other good way to use MI is to pull in an implementation of an
interface. A good example would be a C++ equivalent of
NSKeyValueCoding::DefaultImplementation. Take care though to only
pull that into the most-root object in the hierarchy. That is, since
EOCustomObject pulls it in, your subclass should not. What trips
people up is that it's so easy to pull in the implementation class
that they sometimes fail to consider that it's not appropriate.
The obsession the industry has with hacking good and clean concepts
into C-based languages in horrible ways, debasing the simplicity and
power of those concepts has held the programming profession back.
(Hmmm, turning into a Rick Wakeman-style grumpy rant!)
I'm not sure that I would call templates or C++'s implementation of MI
"good clean concepts". They are not and they are not intended to be.
They are language features that the programmer uses to implement the
real concepts with which they are closely related. You use templates
to implement generics, but templates are just glorified macros, not
generics. You use MI to declare you implement an interface, but MI is
not an interface declaration. You use MI to pull in an implementation
but you may find you need to mix that in with a template and a couple
of extra classes to get better results.
If you want to use C++ as if it were Java or Objective-C be prepared
to essentially implement a good portion of those languages' concepts
because the closest thing to them in C++ is the lower-level language
feature you use to implement them and not in and of itself an
implementation of the same concept.
What is most unfortunate is that Java borrowed the C++ syntax which
leads people to the mistaken impression that the two languages are
doing similar things. Nothing could be further from the truth. Java
method invocation is nearly identical to Objective-C messaging and
shares almost nothing with virtual method invocations in C++. Java
generics aren't at all like a "template class"(sic) but are an
amalgamation of the several steps you need to take in C++ to achieve a
similar runtime behavior.
-Dave
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden