[fpc-pascal] Constants in generics
ryan at thealchemistguild.com
Tue Nov 27 14:00:27 CET 2018
> On Nov 27, 2018, at 5:35 PM, Sven Barth via fpc-pascal <fpc-pascal at lists.freepascal.org> wrote:
> 1) kSomeDays is a typed const so I get a load node instead of tsetconstnode, which I need to need to know in order to make the name string. See the TODO’s in tgenericparamdef.create.
> TDay = (Mon, Tue, Wed);
> TDays = set of TDay;
> kSomeDays:TDays = [Mon, Wed];
> d: specialize TMyRecord_Set<integer,kSomeDays>; // this doesn’t work
> d: specialize TMyRecord_Set<integer,[Mon,Wed]>; // this works because I get a set node
> The difference between typed and untyped constants are available everywhere else in the language as well, so I have no qualms (at least for now?) to error out on them.
You mean reject them? The default behavior is to get a type_e_type_id_expected error. They can be included easily but I don’t know how to get the set from the load node (I think it needs further parsing).
> For sets I believe that this should work however (see here: https://freepascal.org/docs-html/current/ref/refse9.html#x21-200002.1 ):
> === code begin ===
> kSomeDays = [Mon, Wed];
> === code end ===
Yes, sets work because I get a setnode which I can operate on.
> Did you try with the generic and the specialization in different units and changing only one of the two? Though you might want to check whether tstoreddef.ppuload (or wherever tdef.genericparas is initialized after a unit load) correctly instantiated the tconstsym instances instead of ttypesym ones. It might already work correctly, but better safe than sorry. 😅
That seems to work. Here is my test:
generic TList<T, const U:integer> = record
const ID = U;
list: array[0..U-1] of T;
procedure dothis (msg:string);
kShortListLength = 128;
TShortIntList = specialize TList<Shortint,kShortListLength>;
procedure TList.dothis (msg:string);
writeln('message:',msg, ' high:', high(ID), ' constsize:', sizeof(ID), ' structsize:', sizeof(self));
More information about the fpc-pascal