Skip to content

FR: Support pointer to array base type with for .. in.

In addition to existing for BaseType in ArrayType, allow for PBaseType in ArrayType. This is both an optimization (the heavier BaseType and/or the hotter the code, the more preferable second version is) and will allow modifying iterated cells without resorting to counter-based for.

{$mode objfpc}
type
	pABC = ^ABC;
	ABC = record
		a, b, c: double;
	end;

const
	ABCs: array[0 .. 9] of ABC =
	(
		(a: 0; b: 0; c: 0), (a: 1; b: 1; c: 1), (a: 2; b: 2; c: 2), (a: 3; b: 3; c: 3), (a: 4; b: 4; c: 4)
		(a: 5; b: 5; c: 5), (a: 6; b: 6; c: 6), (a: 7; b: 7; c: 7), (a: 8; b: 8; c: 8), (a: 9; b: 9; c: 9)
	);

var
	abcp: pABC;

begin
	for abcp in ABCs do
		writeln(abcp^.a);
end.

Also, while not necessarily true for for BaseType in ArrayType (but usually true for it as well), compiling such for PBaseType in ArrayType as

var
	abcp, abce: pABC;
begin
	abcp := pABC(ABCs); abce := abcp + length(ABCs);
	while abcp < abce do
	begin
		... loop body ...
		inc(abcp);
	end;
end;

will use the same 2 registers (for abcp + abce, instead of abcp + counter), but give more compact and faster code.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information