[fpc-pascal] Get TMethod from function reference

Ondrej Pokorny lazarus at kluug.net
Sun Sep 11 10:28:12 CEST 2022


Am 09.09.2022 um 00:49 schrieb Hairy Pixels via fpc-pascal:
>> On Sep 8, 2022, at 11:14 AM, Michael Van Canneyt via fpc-pascal <fpc-pascal at lists.freepascal.org> wrote:
>>
>> It must keep this information somewhere, but this does not mean you can typecast it to a TMethod.
> Indeed. I wonder if this information could be exposed by some method in the interface? It would be useful to introspect the target class like you can do with “of object”.

See the announcement email from Sven to the fpc-pascal mailing list: 
https://lists.freepascal.org/pipermail/fpc-pascal/2022-May/060507.html

They [function references] get their great power from a point that is 
for once *not*
considered an implementation detail: function references are in fact
internally declared as reference counted interfaces with a single
Invoke() method of the provided signature. So the above examples are in
fact declared like this:

=== code begin ===

type
    TProcLongInt = interface(IInterface)
      procedure Invoke(aArg: LongInt); stdcall; overload;
    end;

You should be able to get the Invoke procedure pointer from the RTTI.

Ondrej



More information about the fpc-pascal mailing list