Generics are overrated anyway
You might save a slight bit of time, but I think it'll only end in tears
Generics are overrated anyway
You might save a slight bit of time, but I think it'll only end in tears
Peregrinus, expectavi pedes meos in cymbalis
Nullus norvegicorum sole urinat
Well, they solve some rather sticky problems at the design paradigm level. For example, ages ago, back before classes, I had a number of different internal linked list methods that I created with Objects (think TP 5.5-5.0). If I wanted singly-linked, doubly-linked, circularly-linked, etc options, I had a different object for each combination of options (well, some of them were actually options in the list). When we got classes, it made it easier to manipulate them syntactically, but I still had a number of classes that I had to support so that I could maintain distinctive functionality. Generics solves the last of the problems I had, so I can actually use just one generic TLinkedList class now, but the fact that it is little more than a glorified high-level macro-like extension still doesn't feel like the optimal solution. I think there is still yet more enhancement and integration to be had in the concept, and it will finally be the sweetest syntactic sugar to solve those sticky kinds of problems.
It is definitely nice for some things, but you're right; it's not useful or appropriate to use for many things, even though programmers sometimes go overboard trying to do so.
In Delphi 2009 and above the new Generics.Collections unit is highly useful.
You can declare containers like this:
X : TObjectList<TMyClass>;
It will hold and own the items.
Then iterate like this:
Item : TMyClass;
...
for Item in X do
...
I like this style of generic containers as it saves time typing and removes the need of casting. There is also a hashtable called TDictionary<K,V>.
But I agree that more advanced use of generics can indeed lead to trouble
I will probably only use them for list-like datastructures. It's really handy if you don't have to copy the same code for integers, floats, 2d, 3d and 4d vectors.
Btw, is it possible to use Type Parameter bounds, like java has? It means that you can enforce that your type-parameter T must be a derivative of a specific class, like this:
Code:public class SomeGenericClass <T extends OtherClass> { //.... }
Last edited by chronozphere; 05-01-2011 at 09:54 AM.
Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.
A text worth mentioning: http://sjrd.ftp-developpez.com/tutor...i-generics.pdf.
Answering your question: yes, it is. As you will see in the article, there is few methods to do so. A little extract from the article, which I put below, provides you with an answer:
Code:type TStreamGenericType<T: TStream> = class end;
Ah thanks. I'll read that!
And how about FPC? Does it have this feature too?
Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.
Looks like Paul Ishenin is updating Delphi mode generics in FPC heavily those days, so it might be safe to just use the Delphi generics style for now
Peregrinus, expectavi pedes meos in cymbalis
Nullus norvegicorum sole urinat
Bookmarks