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

Reply via email to