Skip to content

Function references: Self from methods is not captured in Delphi mode

Summary

Self from methods is not captured in Delphi mode functionreferences.

System Information

  • Operating system: Windows
  • Processor architecture: x86
  • Compiler version: trunk
  • Device: Computer

Steps to reproduce

Run the example project.

Example Project

program TestMethodAsFunctionReferenceDelphi;

{$IFDEF FPC}
{$mode delphi}
{$ModeSwitch functionreferences}
{$ELSE}
{$APPTYPE CONSOLE}
{$ENDIF}

type
  TRefProc = reference to procedure(Sender: TObject);

procedure Test(P: TRefProc);
begin
  P(nil);
end;

type
  TMyObj = class(TObject)
  public
    procedure MyEvent(Sender: TObject);
    procedure MyTest;
  end;

var
  Obj: TMyObj;

{ TMyObj }
procedure TMyObj.MyEvent(Sender: TObject);
begin
  if (Self<>Obj) then // solved with ObjFpc mode and Test(@MyEvent); using Self.MyEvent doesn't help either
    Halt(1);
end;
procedure TMyObj.MyTest;
begin
  Test(MyEvent);
end;

begin
  Obj := TMyObj.Create;
  Obj.MyTest;
end.

What is the current bug behavior?

Self is NIL in TMyObj.MyEvent.

What is the expected (correct) behavior?

Self=Obj in TMyObj.MyEvent.

Edited by Sven/Sarah Barth
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information