[fpc-pascal] Re: TLinkedList

Sven Barth pascaldragon at googlemail.com
Tue Feb 14 20:33:39 CET 2012


On 14.02.2012 19:33, Juha Manninen wrote:
> 2012/2/14, Sven Barth<pascaldragon at googlemail.com>:
>> Am 14.02.2012 06:45, schrieb Carver413:
>>> doesn't generics more or less duplicate the class every time you use it
>>> with
>>> different type, and if so would this not cause alot of bloating ?
>>
>> Yes it does. So for embedded systems "use with care" ;)
>
> Oh, that is bad!
>
> C++ templates generate lots of duplicate code.
>
> Java's generics do not because it is just "syntactic sugar" allowing
> the compiler to check types which IMO is enough for most purposes. You
> don't need ugly type-casts but code-generation is not affected.
> Perfect.
>
> I know Delphi's and FPC's generics are different but I believed the
> compiler is clever enough to use the same class when possible. For
> example, in all cases where objects inherited from TObject are stored
> in a container, the same container could be used.

Such logic unnecessarily complicates the compiler. Generics are already 
troublesome enough.

> This indirectly answered another question I had in mind: Does a
> generics container for a char or integer use the minimum needed amount
> of memory?

If the container uses thinks like "SizeOf(T)" (where T is the generic 
type parameter) and such then it does use the least amount of memory. A 
specialization is basically a reparsing of the generic as if T was the 
specialization type all along.

> I guess it does when a new instance of the container class is generated.

Sorry, but I don't understand what you mean here.

As long as one knows of the disadvantages of generics there is no 
problem in enjoying their advantages.

Regards,
Sven



More information about the fpc-pascal mailing list