Skip to content

Function references compiler error: inherited protected method cannot be passed to a function reference in Delphi mode

Summary

When passing an inherited protected method to a function reference in Delphi mode, the compiler wants to execute the method instead to pass it as parameter.

In ObjFPC mode it works fine. It works fine also when the inherited method is public.

System Information

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

Steps to reproduce

Run the example project below - you get the TestInheritedFunctionReferencesDelphi.lpr(44,8) Error: Wrong number of parameters specified for call to "MyEvent" error message

Example Project

program TestInheritedFunctionReferencesDelphi;

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

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

procedure Test(P: TRefProc);
begin
  Writeln('test');
end;

type
  TMyObjA = class(TObject)
  strict protected
    procedure MyEvent(Sender: TObject);
  end;

  TMyObjB = class(TMyObjA)
  public
    procedure MyTest;
  end;

{ TMyObjA }
procedure TMyObjA.MyEvent(Sender: TObject);
begin
  Writeln('hello');
end;

{ TMyObjB }
procedure TMyObjB.MyTest;
begin
  Test(MyEvent); // solved with ObjFpc mode and Test(@MyEvent); using Self.MyEvent or TRefProc(MyEvent) doesn't help
end;

var
  O: TMyObjB;
begin
  O := TMyObjB.Create;
  O.MyTest;
end.

What is the current bug behavior?

Compiler error.

What is the expected (correct) behavior?

No compiler error.

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