The type of your specialised  class is set at compile time.  However you can 
have multiple specialisations for a single generic class.

One of the obvious uses for generics is lists and container classes. E.g. in 
the Generics.Collections unit in D2009 there are a bunch of classes such as 

  TList<T> = class(TEnumerable<T>)  
  TQueue<T> = class(TEnumerable<T>)
Etc

You can create a specialised list(s) simply by going:
var
  MyList: TList<TMyClass>;
  MyOtherList: TList<TMyOtherClass>;
  MyYetOtherList: TList<TMyYetOtherClass>;

  MyList now has all the methods of a list, specialised to TMyClass.  Ie Add 
will only take a TMyClass, Items will only take and return a TMyClass,  
enumerators will use TMyClass ...  All this happens without any required 
typecasting.


I use this with the tiOPF object persistence framework.  In tiopf, a complete 
container class takes about 100 loc for basic functionality.  This can be 
replaced with a single line of code using generics.  I have 13 such containers 
in my project.





Regards
 
Sean Cross
CIO
Catalyst Risk Management

-----Original Message-----
From: delphi-boun...@elists.org [mailto:delphi-boun...@elists.org] On Behalf Of 
Robert Meek
Sent: Sunday, 26 April 2009 4:44 p.m.
To: delphi@elists.org
Subject: Generics in D2009

        Even though I had done some limited work using Oxygene, ( now Delphi
Prism ), I never got into or even tried to make use of Generics.  There was
too much to learn in .NET already so I was saving them for after I became
comfortable with it.
Now however, and since Delphi has gone to a lot of trouble to include them,
I figured it was time to take a good look at them.  Once again though,
Cantu's D2009 handbook and his theoretical approach to Generics has left me
a little cold.
        It's easy enough to create and instantiate a Generic Class all
right, but at the same time, the mere fact that once a Generic Type has been
set for that class you cannot alter it leaves me wondering exactly when and
why I would put this language feature to practical use...if of course, that
is truly how Generics work!   
        In this example:

type
TSampleClass <T> = class
private
data: T;
public
function GetDataSize: Integer;
function GetDataName: string;
end;
function TSampleClass<T>.GetDataSize: Integer;
begin
Result := SizeOf (T);
end;
function TSampleClass<T>.GetDataName: string;
begin
Result := GetTypeName (TypeInfo (T));
end;
end;

        Am I correct in assuming that 'data : T;' cannot be altered to a
different type once the class is created and a value/Type is given to 'T',
because that is how it came across to me despite it not seeming to make much
sense!  If such is the case why would I even bother using a Generic Class?
        If on the other hand, I created an instance of the class and gave
'T' a value of '0' or 'nil', depending upon the type of data I wish to pass
it, then I could write a procedure that first sets the Value/Type of 'T' to
any I want to at the moment, and then call the three class functions from
within, it would be possible for me to reset 'T' to a different Value/Type
each time I call this procedure!  THAT I can see as being very useful!  
        Which of these is true?  I'm hoping I missed something along the way
and the latter description of a Generic Class's use is correct or at least
closer to the truth!



from "Robert Meek" dba "Tangentals Design"
Creative Concepts Programming for Windows Vista
E-mail:  ffo...@comcast.net
"Most people would sooner die than think; in fact, they do so!"
        [Bertrand Russell  1872-1970]


_______________________________________________
Delphi mailing list -> Delphi@elists.org
http://lists.elists.org/cgi-bin/mailman/listinfo/delphi
_______________________________________________
Delphi mailing list -> Delphi@elists.org
http://lists.elists.org/cgi-bin/mailman/listinfo/delphi

Reply via email to