[fpc-pascal] Constants in generics

Ryan Joseph ryan at thealchemistguild.com
Wed Nov 14 03:25:24 CET 2018


I think I have this done except for cleanup. Here’s the status:

- Integer,string,real,set and nil constant for parameters.
- Consts can have type restrictions which correspond to the above types.
- Const can be assigned to from generic const params.
- PPU loading.

Technical compiler issues:

- I couldn’t figure out how to extend PPU loading for a ttypesym subclass so I put 2 fields into ttypesym. Those are probably best moved to a subclass later.
- We may need new error messages but I just left place holders in for now.

https://github.com/genericptr/freepascal/tree/generic_constants

program generic_constants_types;
type
	generic TList<T, const U:integer> = record
		const
			ID = U;
		public
			list: array[0..U-1] of T;
			procedure dothis (msg:string);
	end;

procedure TList.dothis (msg:string);
begin
	writeln('message:',msg, ' high:', high(ID), ' constsize:', sizeof(ID), ' structsize:', sizeof(self));
end;

type
	TDay = (Mon, Tue, Wed);
	TDays = set of TDay;
const
	kSomeDays:TDays = [Mon, Wed];
var
	a: specialize TList<integer,10>;
	//b: specialize TList<integer,'foo'>;
	//c: specialize TList<integer,0.1>;
	//d: specialize TList<integer,kSomeDays>;
	//e: specialize TList<integer,nil>;
begin
	a.dothis('hello');
end.


Regards,
	Ryan Joseph




More information about the fpc-pascal mailing list