tags:

views:

469

answers:

6

Hi.

This works:

constructor TMyObj.Create;
begin
 inherited;
end;

Why this is not working also?

function TMyObjEx.Import(CONST FileName: string; CONST x, y, z: Integer): string;
begin
 result:= inherited; // Import(FileName, x, y, z);  <--- Compiler says: "incompatible types"
 //do other stuff here
end;

Declaration of TMyObjEx is like this:

TYPE

TMyObj = class(TChObj)
      private
      protected
      public
       function Import (CONST FileName: string; CONST x, y, z: Integer): string; virtual;     
     end;

TMyObjEx= class(TMyObj)
          private
          protected
          public
           function Import(CONST FileName: string; CONST x, y, z: Integer): string; override;   
         end;
+1  A: 

At first I thought you could just use inherited if you were uninterested in the function's result, that that appears to not be the case. Calling inherited function methods requires the method name and parameters. That's just the way it is. You also need to mention the method name if you're passing different parameters from the ones the current method received, or if you're calling an entirely different method.

Rob Kennedy
+5  A: 

The automatic parameters passing just does not work when you need the result of the method. You need to fill in the name of the method and the parameters, sorry.

Lars Truijens
A: 

Seems the Delphi documentations have another black hole... (Oooooh, what's the great news on it?)

My tests indicate that the use inherited keyword without anything after it works only on constructor, destructor and procedure methods. On Function methods it does not work AT ALL.

However, I never detected it because I have the habit of calling the inherited method in full, even when I don't need it (motivation: to be able to use Ctrl+Left click on it and follow the flow of the code easier without debugging).

Fabricio Araujo
+2  A: 

Here's the correct answer.

The proper way to do this is as you've noted above:

function TMyObjEx.Import(CONST FileName: string; CONST x, y, z: Integer): string;
begin
 result:= inherited Import(FileName, x, y, z); 
 //do other stuff here
end;

The way you are wanting to do it is not supported by the language.

So ultimately the answer to your question of "Why does not this work also?" is because that is not the way the language is designed.

Nick Hodges
Well OK, I knew that, but why? ;)(seriously, I'd be interested in the low-level technical reason why this works for procedures but not for functions)
Oliver Giesen
To do it the way you were trying requires a much higher level of "assumption."
Jim McKeeth
But this is a natural assumption. It works for everything, except for functions - and when you look on the documentation you it refers to 'methods', which for me means function, procedures, constructor and destructors.
Fabricio Araujo
A: 

Thanks Fabricio Araujo. So, is not a bug in my program but a bug in Delphi documentation.

Another few hours well wasted. Thanks Borland. :)

A long one, D5, D7 and D2006 have the exact same misleading text in the Delphi Language Guide (yeah, I know in D5 it's called Object Pascal Guide).
Fabricio Araujo
+1  A: 

As to why it is not supported, Hallvard wrote a plausible explanation few years ago in his blog:

One caveat with the "inherited;" syntax is that it is not supported for functions. For functions you must use the explicit syntax including the method name and any arguments. For instance:

[some code]

This might look like an oversight in the Delphi language design, but I think it is deliberate. The rationale behind it is probably that if TMyClass.MethodC is abstract (or made abstract in the future), the Result assignment in the descendent class will be removed, and thus Result has suddenly undefined value. This would certainly cause subtle bugs.

gabr