[fpc-devel] Packages, Generics

Willibald Krenn Willibald.Krenn at gmx.at
Sun Sep 12 12:40:04 CEST 2010

Marco van de Voort schrieb:
> No it won't. Not all of the implementation of a package needs to be in the
> .BPL equivalent. Neither is the case in Delphi. 
> Seek for $weakpackage and read the packages wiki again

In words of Borland/Embac - "Unit files containing the {$WEAKPACKAGEUNIT 
ON} directive must not have global variables, initialization sections, 
or finalization sections." Which makes sense. So not putting something 
into the bpl comes at a cost.

Also, I don't see the benefit of trying to introduce packages to have 
each type defined only once within the project, except that all generic 
types (and descendants thereof) would be spread all-over again.

> So in short, everything in the units that doesn't go into the DLL goes into
> the .DCP which is more or less a dcu archive if I understood it correctly
> So only the specializations used inside the code contained in the BPL (and
> maybe one or two that you could force to save duplication) are in the BPL.
> So in short, the problem is one that you create yourself by self-inflicting
> that every piece of code _must_ be in the BPL. That isn't the case.

Ok, so lets say one of my generic classes uses class variables (static 
fields). Duplicating the type in each package defeats the purpose of the 
package-concept to have one common type across all modules of a running 

Say I have a generic TMyList<T> type with some class variable. Then, I 
use the specialization TMyList<TMyOtherClass> in two different packages 
that depend on the package where TMyList<T> was defined. As soon as 
TMyList<T> is put into the .dcp, it will be statically compiled into the 
two other packages - hence you end up with 2 "instances" of the same 
type, which leads to having 2 different versions of the class variable 
(and N in the general case). In my opinion this is not what packages are 
about. Packages are a means to transparently break your application in 
to modules. BTW: It would be intersting to know whether Delphi allows 
some sort of "type TMyNewList<T> = type TMyList<T>" definition  which 
would create a new type with a different set of class variables.

Finally, I have to confess that I am absolutely clueless about the 
semantics of class variables in FPC's generics. Most probably FPC only 
shares class variables between all instances of the same "type mySpec = 
specialization TMyList<...>" type, which would give somewhat room for 
the .dcp implementation. But this is not the usual semantics of 
generics. It also is not the Delphi semantics of generics, I suppose. 
This is (C++) templates without header files.


More information about the fpc-devel mailing list