Skip to content

Overloaded methods cannot be properly assigned as an event handler

Summary

An overloaded method can only be assigned to an event handler if the "correct" method (i.e. the one with parameters matching the event) is declared before the alternatives having non-matching parameter lists.

System Information

  • Operating system: Windows
  • Processor architecture: x86-64
  • Compiler version: 3.2.2
  • Device: Computer

Steps to reproduce

see example below

Example Project

program Project1;

uses Classes;

type TTest = class(TObject)
 procedure SomeEvent (Sender: NativeInt); overload;
 procedure SomeEvent (Sender: TObject); overload;
end;

procedure TTest.SomeEvent (Sender: TObject);
begin
end;

procedure TTest.SomeEvent (Sender: NativeInt);
begin
end;

var
 x: TTest;
 y: TStringList;

begin
 x := TTest.Create;
 y := TStringList.Create;
 y.OnChange := @x.SomeEvent;
end.

What is the current bug behavior?

The code shown above fails to compile: project1.lpr(25,17) Error: Incompatible types: got "<procedure variable type of procedure(NativeInt) of object;Register>" expected "<procedure variable type of procedure(TObject) of object;Register>"

When swapping both method declarations (so that SomeEvent(Sender: TObject) is declared first), it works.

What is the expected (correct) behavior?

The compiler should be able to choose the correct variant of the overloaded method regardless of the order of declaration.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information