[fpc-pascal] linked list to criticize + generics

Juha Manninen juha.manninen at phnet.fi
Mon May 3 09:59:54 CEST 2010


> I'm curious, why didn't you make it a class...wouldn't that have made it
> nicer/easier to use and program?

+1
It should be a class, yes. Now you have loads of global functions.

The integer data type doesn't make much sense but your comment says it will be 
changed to generics type. That sounds good.

Container classes are maybe the only place where generics really are useful.
I often derive a list from TObjectList and redefine its "Items" property and 
"GetItem" + "SetItem" methods just to get a compiler type checking for correct 
type and to avoid type-casting later in code, and to benefit from Lazarus' 
hint and code completion features. Generics would do the same thing easier.

Once I had to maintain a Java program made without generics. The first thing I 
did, even before understanding the code much, was to define generics type for 
all containers by guessing it from type-casts. It helped me to understand the 
code better and to avoid errors later when changing it. I even spotter clear 
errors (wrong type-casts) in the original code. With Eclipse's hints and code 
completion it was a huge improvement.

Object Pascal generics containers + Lazarus would give the same benefits.

If someone is working on the generic containers this linked list effort should 
be added there (after making it a class and adding generics support).
Actually it should be coordinated somehow because the containers could share 
some code. Delphi 2009 introduced generic containers which have some clever 
code. I think it needs effort to support native types like string and arrays. 
Supporting only class types (TObject->) would be easier.

Without generics support the linked list data type could be TObject which lets 
you forget the pointer syntax. Sometimes it causes overhead but it is very 
small compared to virtual machine overhead (Java, .NET etc.) while the syntax 
is about equally clear.


Regards,
Juha



More information about the fpc-pascal mailing list