[fpc-pascal] compile time init'ing records

Ryan Joseph ryan at thealchemistguild.com
Fri Apr 13 05:02:49 CEST 2018

It’s a real pain setting default values for records in Pascal at compile time. Every other language seems to get this right but it’s a constant issue in Pascal. The new "class operator Initialize” feature goes a long way in helping this but it’s still a little annoying that the function is down in the implementation and taking up space plus requires scrolling etc… Static class methods help also but again the function is detached from the record and needs to be called manually.

Why can’t we just do something simple like this? A simple meta programming hack to dump the “default” section at the start of the scope the variable is declared in so we don’t have to do this ourselves every time. It probably breaks down for init’ing records but for compiler types it seems pretty simple.

	TMyRecord = record
			i: integer;
			s: string;
			i := 1;
			s := 'foo';

	myRec: TMyRecord; default; // if no default section is specified then use FillChar(this, sizeof(this), 0);
	// the compiler copy/pastes the variables "default" section at start of the scope
	myRec.i := 1;
	myRec.s := 'foo';

	Ryan Joseph

More information about the fpc-pascal mailing list