[fpc-devel] Generics Basics

L505 fpc505 at z505.com
Tue Nov 8 23:28:11 CET 2005


>
> The Very Big Advantage (Tm), is that you get syntax checking, while still
> using a type diversely. That's impossible to do (at compile-time) without
> generics.
>
> Probably the best example of this is something like TList:
>
> Without generics:
>
> TOrange = class ... end;
> TApple  = class ... end;
>
> var
>   T: TList;
> begin
>   T := TList.Create();
>   T.Add(TApple.Create());
>   T.Add(TOrange.Create());
>   // Whoops, what was that? Now our program might crash.
> end;
>
> Of course, we could create a specialized TAppleList that only accepts Apples,
> but that's boring and hard work. A much cleaner way is generics:
>
> var
>   T: TList<TApple>;
> begin
>   T := TList<TApple>.Create();
>   T.Add(TApple.Create());
>   T.Add(TOrange.Create());
>   // This wont compile! The problem is prevented at compile-time!
> end;
>
> I hope that answers your question as to why it's a good idea :-)
>
> --
> Regards,
> Christian Iversen


Sort of, but I don't see why people use TApple and TOrange examples all the time ;-) I mean
when was the last time you actually programmed a software application that was organizing
Apples and Oranges? I use things like strings, databases, integers, FTP, CGI, stringlists,
etc. Sure apples and oranges can represent a sort of metaphor for ANYTHING, but I'd prefer
just to cut straight to the real world example instead.

I guess the real world examples are hard to display unless you have actually used generics
in a software program, and you are willing to offer code snippets from the program.

I wish for example, I could see someone using something more real world such as:

-----BEGIN OF CASE STUDY-----
         (just a fake one)

---Description---
  I used generics in my FTP uploader software application and it prevented
  an error for me.

---Code examples---
  Here are some code examples taken right from my real world ftp
  application written in C++. Since Pascal doesn't have generics currently,
  this example is in C++ only to help you see  why they are useful in an
  existing software application.
  ..............
  ......insert code here ....
  ..............

---Conclusion---
  My executable size is now 1400KB whereas before when not using
  generics it was 1340KB. The 60KB gain was not a big deal. My
  conclusion is that generics offer significant advantages, but they
  shouldn't be used in some situations.

 If Pascal implements generics I estimate my executable will be about
1400KB too, and the syntax will be as follows if I was to convert
 my C++ application to Pascal using generics:
  ......
 ...insert code here...
  .....

---Other notes:---
  Generics don't work well in the following situations:
    A)....using a type diversely can cause issues when... ....
    B).....abusing generics could cause issues if you if... ...
    C).....generally they shouldn't be used if.... ...


-----END OF CASE STUDY-----


However, I suppose I'm idealistic and too much of a real world guy some times. Case studies
can be perfect if you have software program already in existence, or if you can create a
small demo program that is fully functional and actually does something real on the
computer.  I helps more easily prove the advantages of a certain programming. paradigm. Then
there are video case studies, but I won't get in to those since those are more to do with
demonstrating visual advantages of GUI's and software features.




More information about the fpc-devel mailing list