Hi all,
FPC trunk (3.3.1) has a new compile time operator:
type of <operand>It gives the type of a variable, field, property, constant, function result or expression, and works wherever a type is allowed. The operand is only type checked, never evaluated.
It is on by default in
{$mode objfpc} and
{$mode extendedpascal}. In other modes use
{$modeswitch typeinquiry}.
It is useful to refer to anonymous, combined or derived types.
Examples:
{$mode ObjFPC}
var
Count: word;
List: array of type of Count; // = array of word
Sum: type of (Count+Rec.X); // type of an expression
v: type of Foo(SomeByte); // result type, Foo is not called
type
TUniqueCount = type type of Count; // distinct type
generic function Add<S,T>(a: S; b: T): type of (S+T);
TRec = record
a: int64;
b: type of a; // sibling field
end;
generic TGiraffe<PT> = class
public type
T = type of PT^; // TGiraffe<PWord>.T = word
end;
x := type of Count(SomeLongint); // type cast
n := SizeOf(type of Count);
Notes:
- "type of Integer" is an error: the operand must not be a bare type.
- "type of a+b" means "(type of a)+b". Write "type of (a+b)".
- In generics the operand is resolved at specialization.
fcl-passrc and pas2js support it too.