Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it supposed to be possible for methods to be missing their arguments in the implementation?

I face some common IDE bugs in Delphi XE2 (RAD Studio) but the problems themselves aren't my concern. It's the result of one of these bugs which made me stumble on something else.

Somehow, auto-completion decided to destroy a few methods of a form, so what used to be...

procedure TForm1.Button1Click(Sender: TObject);

in the implementation became something like...

procedure TForm1.Buproced(Sendure :);

(Not exact, but to some extent like that)

So, I had to manually fix these methods. However, I accidentally fixed one of them to...

procedure TForm1.Button1Click;

although it was supposed to have been...

procedure TForm1.Button1Click(Sender: TObject);

yet it still compiled and ran fine.

To test, start a new VCL Forms Application and drop just one TButton control, make an event handler for OnClick, and change its procedure to...

procedure TForm1.Button1Click;
var
  B: TButton;
begin
  B:= TButton(Sender);
  B.Caption:= 'Something';
end;

Is this supposed to be possible? Or is it perhaps an IDE and/or compiler bug?

like image 661
Jerry Dodge Avatar asked Jan 13 '23 19:01

Jerry Dodge


2 Answers

In Delphi, you can omit the parameters in the implementation. It's not a bug, it's a feature.

The proper method signature is evaluated by the declaration in interface section.

like image 135
nullptr Avatar answered Jan 29 '23 15:01

nullptr


This is an intentional and documented feature of the language. This is the part of the documentation that describes this feature, with my added emphasis:

While a class can be declared in either the interface or the implementation section of a unit, defining declarations for a class methods must be in the implementation section.

In the heading of a defining declaration, the method name is always qualified with the name of the class to which it belongs. The heading can repeat the parameter list from the class declaration; if it does, the order, type, and names of the parameters must match exactly, and if the method is a function, the return value must match as well.

like image 43
David Heffernan Avatar answered Jan 29 '23 15:01

David Heffernan