[fpc-devel] Packages, Generics

Willibald Krenn Willibald.Krenn at gmx.at
Sun Sep 12 21:38:33 CEST 2010


Marco van de Voort schrieb:
> No, since any specialization creates the actual types, and they all will
> have their respective static fields anyway?  (since the static field can be
> of the variable type)
> 
> There are some languages/runtimes that have some damage control here (most
> notably C# which uses constraints to limit the definition to classes
> implementing a certain interface, and it is said to typically share
> specializations for such classes)

Played around with Delphi today, and it seems that class vars are 
somewhat broken on generic types. They work as expected (share common 
class value amongst all sameclass<sametype> instances) only in a unit 
scope, it seems. Which is a pitty.

However, Delphi won't allow you to do something like

TMyGenericBaseClass<T> = class
   public
       public function AddOp(a: T; b: T) : T;
end;

implementation

function TMyGenericBaseClass<T>.AddOp;
begin
   result := a + b;
end;

since the '+' operator is not defined on T. Delphi lets you constrain 
your type parameters. That said, I don't really understand why 
constraints like :integer, :TObject are not allowed. (:record, :TMyClass 
are perfectly fine.) So Delphi's generics are more on the C#/.NET side 
than on the C++ template side.

>> Packages are a means to transparently break your application in 
>> to modules.
> 
> True, the specialization is what generates the code AND THAT IS NOT IN THE
> UNITS IN THE PACKAGE! So the logic still holds.

Uh, no need to shout. I understand what you are saying, however, I'd 
still prefer to have working class variables, rtti checks etc. even with 
generic classes and packages.

>> 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.
> 
> You can try, but I doubt it. The = type <x> functionality is mostly
> sleeping, in both FPC and Delphi.

Tried it: not allowed.

> The only exception I know is C#'s constraints, but that is possibly nothing
> but a bit of damage control in a totally different environment.  Still it
> might be possible for FPC too, but that is a totally different subject.

Well, IIRC Eiffel, Java/Pizza, .NET, Delphi all support constraints on 
type parameters. .NET actually has quite good support for generics (Java 
is limited in that respect).

> When I read up about the C# stuff I didn't know that much about generics
> yet, so if interested, I encourage you to do your own research. But don't
> get your hopes up too much.

Ok for the start I'll be looking at co/contravariance, and lambda 
functions ;-). Joke aside, I'll think about the .dcp idea and how this 
might work out with packages.

Cheers,
  Willi




More information about the fpc-devel mailing list