Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2026-09

"Type Of" operator

1 message · 1 person ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Mattias Gaertner

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

Reply by email