[fpc-pascal] Constants in generics
pascaldragon at googlemail.com
Tue Nov 27 14:11:35 CET 2018
Am Di., 27. Nov. 2018, 14:00 hat Ryan Joseph <ryan at thealchemistguild.com>
> > 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
> > 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).
We can add a new message that says "type id or untyped constant expected".
But if it correctly errors right now, we can leave it be for now.
> > 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;
> a: TShortIntList;
> unit gc_types_unit;
> 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>;
> GlobalShortIntList: TShortIntList;
> procedure TList.dothis (msg:string);
> writeln('message:',msg, ' high:', high(ID), ' constsize:',
> sizeof(ID), ' structsize:', sizeof(self));
You did read the part about the generic and the specialization being
located in two different units? 😉
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the fpc-pascal