[fpc-pascal] Get TMethod from function reference

Hairy Pixels genericptr at gmail.com
Sun Sep 18 02:50:16 CEST 2022



> On Sep 17, 2022, at 10:57 PM, Sven Barth <pascaldragon at googlemail.com> wrote:
> 
> === code begin ===
> 
> type
>   TProc = reference to procedure;
>   TMyClass = class
>     procedure DoThis;
>   end;
> 
>   TCapturer = class(TInterfacedObject, TProc)
>     m: procedure of object;
>     procedure Anonymous1;
> 
>     procedure TProc.Invoke = Anonymous1;
>   end;
> 
> var
>   p: TProc;
>   c: TMyClass;
>   capturer: TCapturer;
>   capturer_keepalive: IUnknown;
> begin
>   capturer := TCapturer.Create;
>   capturer_keepalive := capturer;
>   c := TMyClass.Create;
>   capturer.m := @c.DoThis;
>   p := capturer as TProc;
> end.
> 
> === code end ===

Well if p is effectively TCapturer then you COULD get back TMethod from m (procedure of object) but the compiler doesn’t have a method generated for this.

I read your announcement email again last night and saw that TProc could actually be subclassed which opens up the possibility to read the field (if it’s not intentionally hidden) or use the RTTI perhaps even. Is that possible? If the field “m" is hidden I would say that should be exposed for the purposes of subclassing .

Regards,
	Ryan Joseph



More information about the fpc-pascal mailing list