[fpc-pascal] Constants in generics
Ryan Joseph
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.
>
> type
> TDay = (Mon, Tue, Wed);
> TDays = set of TDay;
> const
> kSomeDays:TDays = [Mon, Wed];
>
>
> var
> 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 ===
> const
> 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:
========================================
program gc_ppu;
uses
gc_types_unit;
var
a: TShortIntList;
begin
a.dothis('hello’);
GlobalShortIntList.dothis('world');
end.
========================================
unit gc_types_unit;
interface
type
generic TList<T, const U:integer> = record
const ID = U;
public
list: array[0..U-1] of T;
procedure dothis (msg:string);
end;
const
kShortListLength = 128;
type
TShortIntList = specialize TList<Shortint,kShortListLength>;
var
GlobalShortIntList: TShortIntList;
implementation
procedure TList.dothis (msg:string);
begin
writeln('message:',msg, ' high:', high(ID), ' constsize:', sizeof(ID), ' structsize:', sizeof(self));
end;
end.
Regards,
Ryan Joseph
More information about the fpc-pascal
mailing list