[fpc-devel] Templates / Generics Syntax
peter at freepascal.org
Fri Nov 4 14:45:01 CET 2005
> Micha Nelissen wrote:
>> TGenericCollection = generic(T: TCollectionItem) class(TComponent)
>> ...implement TCollection and use T
>> TCollection = TGenericCollection of (TCollectionItem);
>> TFieldDefs = TGenericCollection of (TFieldDef);
> So generic procs could look like:
> function generic(T: TTreeItem) MyFunc(A: T): T;
> // do something with the tree item
> My restrictions won't allow implementing generic Max and Min, I guess.
> That really needs macro-alike stuff (for the compiler implementation).
> The syntax could look like:
> function generic(T) Max(A, B: T): T;
> if A < B then
> Result := B
> Result := A;
I did some tests with g++. It looks like it parses the template 'normally'
and don't handle it like a macro. When instantiating the template the
generic type is replaced.
I don't know if that is possible with FPC. The parser relies a lot on
types being available. Writing a special parser for generics is possible.
It can then store a simple expression/statement tree that will generate a
full node tree when the generic is instantiated. But that means that it
must be known that
More information about the fpc-devel