Reputation: 167
I would need to write a function that performs operations in RTTI mode on data, of which I do not know a priori the type.
I tried to write a function like this:
function doSomething (T: TypeIdentifier): Boolean;
as when using the expression TypeInfo(T)
he expects as a type parameter TypeIdentifier
But when I try to compile I get an error because the type TypeIdentifier not recognized.
Someone could explain me how I can do to send a data type of which I know the type only at runtime?
Thanks to the availability.
Enzo
Upvotes: 3
Views: 908
Reputation: 613461
TypeInfo
is an intrinsic function that uses compiler magic. That is the compiler generates the code that implements the function rather than it being part of the runtime library.
You don't have access to such magic. You cannot create a function that accepts a type identifier in the manner of TypeInfo
. So you need to make your function accept what TypeInfo
returns, a pointer to the type info, PTypeInfo
.
You'd call your function like this:
DoSomething(TypeInfo(SomeTypeIdentifier));
Now technically, you may notice that TypeInfo
returns a value of type Pointer
. That's because PTypeInfo
is defined in another unit, TypInfo
and the System
unit where all intrinsics are defined is not allowed to use TypInfo
. But as stated in the documentation linked above, TypeInfo
returns a pointer to TTypeInfo
.
Upvotes: 3