Skip to content

Compiler does not accept “specialize” inside “const” (but does the expected thing when “specialize” or “const” are implicit).

This code does not compile:

{$mode objfpc} {$modeswitch advancedrecords}
type
	generic AlignOf<T> = record
	private type
		Ofs1 = record b: byte; v: T; end;
	public const
		Value = PtrUint(@Ofs1(nil^).v);
	end;

// (12,16) Identifier not found "specialize"
const
	CsAlignment = specialize AlignOf<TRTLCriticalSection>.Value;

begin
end.

Yet the $mode delphi equivalent works perfectly:

{$mode delphi}
type
	AlignOf<T> = record
	private type
		Ofs1 = record b: byte; v: T; end;
	public const
		Value = PtrUint(@Ofs1(nil^).v);
	end;

const
	CsAlignment = AlignOf<TRTLCriticalSection>.Value;

begin
end.

Moreover, even $mode objfpc accepts e.g.

{$mode objfpc} {$modeswitch advancedrecords}
type
	generic AlignOf<T> = record
	private type
		Ofs1 = record b: byte; v: T; end;
	public const
		Value = PtrUint(@Ofs1(nil^).v);
	end;

	MyType = record
	end align specialize AlignOf<TRTLCriticalSection>.Value;

begin
end.

which should by no means differ from const.

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