Hi,
I think what got me hung up was the statement in Cantu's book that
"once a Generic type has been set for that class you cannot alter it". But
though true, this is not done when the class is created, but when it is
instantiated. All one needs to do is create a new instance of the class and
then one can set its type. I knew that had to be the case, but his
discussion got my wires crossed up a bit. That's why I had to ask and make
sure!
from "Robert Meek" dba "Tangentals Design"
Creative Concepts Programming for Windows Vista
E-mail: [email protected]
"Most people would sooner die than think; in fact, they do so!"
[Bertrand Russell 1872-1970]
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf
Of Stephen Posey
Sent: Sunday, April 26, 2009 3:49 PM
To: Borland's Delphi Discussion List
Subject: Re: Generics in D2009
Robert Meek wrote:
> 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!
>
Your statements say to me that you're missing the point and value of
generics: they allow you to write /generic /code in a type safe fashion.
The typical use of generics involves common data structures (e.g.
queues, stacks, trees), with generics these can be written once using
type generic code such as you describe, and then be applied to whatever
sort of data might be desirable to store in them without having to fully
specify the types ahead of time.
Type specification occurs when the generic is /instantiated/.
Instantiation generates a new type that cannot be used with anything but
the type(s) specified at instantiation (type safety).
This is in contrast to older approaches that involved collections of
pointers or object references that could be used to store /anything
/(potentially leading to hard to track type bugs), or creating custom
descendants or wrappers to restrict the types (a repetitive and labor
intensive process with much code duplication).
Does that help?
Stephen Posey
[email protected]
_______________________________________________
Delphi mailing list -> [email protected]
http://lists.elists.org/cgi-bin/mailman/listinfo/delphi
_______________________________________________
Delphi mailing list -> [email protected]
http://lists.elists.org/cgi-bin/mailman/listinfo/delphi