[fpc-pascal] Generics problem/question
Aleksa Todorovic
alexione at gmail.com
Tue Jan 19 18:35:04 CET 2010
On Mon, Jan 18, 2010 at 04:07, Aleksa Todorovic <alexione at gmail.com> wrote:
>
> The proper solution for this problem is not simple. Somehow, you will
> have to make operator = (const A, B: TPar) visible inside FGL unit
> (because of the way generics are currently implemented), or make
> compiler think that TPGList is implemented in the place where
> specialization occurs (so compiler first searches "specialization
> space", and after that "generic-declaration space").
>
One (not very nice) way to have generic list of records is using
macros. I've extracted definition of TFPGList, made it ordinary type,
and did some search/replace, so I get:
type
_TFPGList_Type_ = class(TFPSList)
type public
TCompareFunc = function(const Item1, Item2: _TFPGList_ItemType_): Integer;
TTypeList = array[0..MaxGListSize] of _TFPGList_ItemType_;
PTypeList = ^TTypeList;
PT = ^_TFPGList_ItemType_;
[original code from FGL unit goes here]
end;
constructor _TFPGList_Type_.Create;
begin
[...]
end;
[...]
Now, you insert code like this:
type
TPar = record
I: Integer;
end;
operator = (const A, B: TPar): Boolean;
begin
Result := (A.I = B.I);
end;
{$MACRO ON}
{$DEFINE _TFPGList_Type_ := TParList}
{$DEFINE _TFPGList_ItemType_ := TPar}
{$INCLUDE fpglist.inc}
{$MACRO OFF}
And you get list of TPar records!
Now, The Question is: should "specialize" construct also search
symbols defined in the place where specialization occurs, not just
those defined in the place where generic type is defined?
On the other hand, the only problem so far (regarding this matter) is
operator = on records. Maybe there should be special rule for this
situation?
More information about the fpc-pascal
mailing list