[fpc-pascal] "Type Of" operator

Mattias Gaertner nc-gaertnma at netcologne.de
Mon Sep 28 14:15:00 CEST 2026


Hi,

fpc now supports the "type of" operator - a compile time operator which 
returns the type of a variable, const, argument, property, function 
result or derived types.
It is enabled in modes objfpc and extendedpascal, and can be turned on 
with $modeswitch TypeInquiry.

Examples:

{$mode objfpc}
var
   Int: integer;
   Val: type of Int; // integer

It can be used everywhere a type is possible:

type
   TInt = type of Int; // integer

function Run(a: type of Int): type of Int;
var
   Arr: array of type of Int;
   El: type of Arr[123]; // no range or overflow check is done
   b: byte;
begin
   b := type of b(Val); // type cast
   b := (type of b)(Val); // type cast with brackets
end;

type-of can get function result types:

type
   TFuncRes: type of Run(1);  // integer

Note: The function is not called. The parameters are merely used to 
figure out the right overload.

type-of can be used to get the combined type:

type
   generic TSum<S,T> = record
     Sum: type of (S+T); // e.g. S=double, T=single, Sum: double
   end;

And the dereferenced type:

type
   generic TGen<aPointerType> = record
     BaseType: type of aPointerType^; // e.g. BaseType=Word for PWord
   end;



Mattias



More information about the fpc-pascal mailing list