[fpc-pascal] Constants in generics

Ryan Joseph ryan at thealchemistguild.com
Thu Nov 8 13:59:32 CET 2018


I added const in declarations so you get syntax error. I also disabled the const’s in delphi mode because it does indeed break inline specializations. Not sure how difficult that will be to fix or if it’s even worth it since this feature isn’t in delphi afaik.

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

Pointless test to show the syntax:

{$mode objfpc}
{$modeswitch advancedrecords}
{$interfaces CORBA}

program generic_constants_interfaces;

type
	generic IMyInterface<T, const U> = interface
		procedure DoThis (value: T);
	end;

type
	generic THelper<T, const U> = record
		list: array[0..U-1] of T;
	end;

type
	generic TMyClass<T, const U> = class (specialize IMyInterface<T, U>)
		type
			// todo: second param in THelper must be const also!
			THelperType = specialize THelper<T, U>;
		public
			helper: THelperType;
			procedure DoThis (value: T);
	end;
	TListClass = specialize TMyClass<integer,10>;

procedure TMyClass.DoThis (value: T);
begin
	writeln('DoThis:',value, ' default: ', U);
end;

var
	c: TListClass;
begin
	c := TListClass.Create;
	c.helper.list[5] := 100;
	writeln(c.helper.list[5]);
	c.DoThis(100);
end.

Regards,
	Ryan Joseph




More information about the fpc-pascal mailing list