[fpc-devel] Generics Basics

Bram Kuijvenhoven kuifwaremailinglists at xs4all.nl
Wed Nov 9 12:22:41 CET 2005


Daniƫl Mantione wrote:
> Ok, lets put it blunt. It is absolutely not important to have templates at 
> all. We've been able to develop top class code of the best kind without 
> templates.

That is true. Yet I really like the type-safety of templates. Advantages include:
- you have to type less type casts
- in an IDE like Lazarus you can directly use code completion (result of the previous item)
- less potential bugs due to storing an object of the 'wrong' class in a container

It will speed up the development process in complex applications where you need all kinds of containers all the time.
And it's cool. And type casts look like hacks :) So perhaps generics can be characterized as a hype, but I think the real advatages of generics are apparent.

I also like dynamic arrays. (Note that they weren't available in FPC in the past.) They make things type-safer too.

People were asking for case studies in this thread. Well, I'm creating a report generator. This is more or less an example of the above mentioned 'complex software where you need all kinds of containers all the time'. For example:
- I'd be very happy to replace all TLists by TList<specific type>s.
- Also, adding caches (hash tables) would be a lot easier. Caches can for example prevent you from including the same resource (image, font) multiple times in the output file.
Motto: No more type casts, only CTRL+Space :)

(Ok, maybe I'm exaggerating a little bit, but don't you agree generics /are/ useful?)

> There's a big risk involved with templates; code bloat. Namely, with 
> templates, you can instantiate classes without realising that you are 
> adding tens of kilobytes of code.
> 
> I.e. in C++ if you instantiate a vector<class_a *>, vector<class_b *>, 
> vector<class_c *>, you have three implementations of the vector in your 
> code, which are all an array of pointers! Surely Pascal's 
> Tcollection/Tlist is much more efficient.

Won't the compiler sometimes be able to handle this smarter? When the code generated for vector<TClassA> and vector<TClassB> is equivalent, we only need to include it once in the resulting executable, right? The only thing is we have to see when this situation occurs.

Things can go wrong when for example
- an overloaded method or operator is called on T
- a method of T is called (but this is no problem when it concerns the same (virtual) method for the actual type parameters under consideration)
- class of T is used
- RTTI info is requested for T

But if I am correct, this is in general not the case for container classes like vector<T> (or: TList<T>).

> Templates can save typing. People are demanding them, because there is a 
> hype. Lack of templates is seen as a deficiency of Pascal against C++.

Somehow people from the well known ACM programming contest seem to see Pascal as a 'deficiency' itself:

>From "The Rules of the 2005 ICPC Regional Contests" (http://icpc.baylor.edu/icpc/regionals/About.htm):

"The programming languages of the regional contest will include C/C++. Additional programming languages may be used. The programming languages of the 2006 World Finals are Java, C/C++, and Pascal.  Prior to the World Finals, the judges will have solved all problems in Java and C/C++, but not necessarily in Pascal.  Pascal is a legacy language that is provided at the World Finals as a courtesy to the teams that are still dependent on it. Pascal will no longer be available starting with the 2007 World Finals."

I think that indeed C++'s STL and Java 1.5 rule over Pascal at the moment for programming contests. Perhaps generics & a nice container library using generics can help getting Pascal back (but I don't know all the considerations of the ACM World Finals' organisers!)

Regards,

Bram



More information about the fpc-devel mailing list