4

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?

Jerry Dodge
  • 26,858
  • 31
  • 155
  • 327

2 Answers2

7

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.

nullptr
  • 11,008
  • 1
  • 23
  • 18
  • 5
    I never used this "feature". It is very misleading, when you read the code. IMHO this is a weakness of the language: I'm not able to see one reason for this behavior, especially with the Delphi IDE which writes the code for you (e.g. with Ctrl+Shift+C from the interface part). It could make sense in Turbo Pascal days... Just confusing now. – Arnaud Bouchez Jul 28 '13 at 19:20
  • @ArnaudBouchez, indeed a weakness. I never saw any reason (except laziness) to make use of this "feature". Not even in the Turbo Pascal days. – LU RD Jul 28 '13 at 19:40
  • Indeed, I always use code completion, ever since I had it available, and since then, I have never written a class method implementation without it. – Jerry Dodge Jul 28 '13 at 19:45
  • @Arnaud The reason is maintenance. Modifications to the parameter list (or even the result type in case of a function!) can be done in the single declaration in the interface section. [I never use it though](http://stackoverflow.com/a/6086712/757830). – NGLN Jul 28 '13 at 20:41
  • What I don't like on Class Completion is that it omits the default parameter values in implementation part of method prototypes. Then you can't see that a certain parameter has a default value on the first view (if you don't complete that default parameter value to the prototype by yourself). – TLama Jul 28 '13 at 21:09
  • 1
    @TLama: Actually, I prefer the default parameters to be missing from the implementation. In my code itself, it doesn't much matter what the defaults are, and if I find I need to modify the defaults during maintenance, then it's one less place to have to look for values. – afrazier Jul 29 '13 at 12:17
2

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490