Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best approach to elevate method visibility in a descendant class

Tags:

delphi

Best way to elaborate on the question is to give an example of what I'm trying to do...

I define a "base class":

TMyBaseClass = class(TPersistent)
protected
  procedure Foo(const AValue: String); virtual;
  // more methods here (many more in fact)
end;

I now define a descendant class:

TMyDescendantClass = class(TMyBaseClass)
public
  procedure Foo(const AValue: String); override;
  // etc. for all desired methods I wish to elevate into Public
end;

The problem here is that I then have to redefine the method "Foo" for TMyDescendantClass to pass the call up the chain to TMyBaseClass:

procedure TMyDescendantClass.Foo(const AValue: String);
begin
  inherited;
end;

This is a waste of space! I'm wondering if anyone knows of any way to negate the need to reimplement the method and acll "inherited".

An ideal solution would look something like:

TMyDescendantClass = class(TMyBaseClass)
public
  procedure Foo(const AValue: String); elevated;
  // etc. for all desired methods
end;

Obviously this is hypothetical, and I know the keyword "elevated" does not exist in the Delphi language. Is there a keyword with the same effect I just don't know about?

Thanks!

like image 227
LaKraven Avatar asked Jan 19 '23 05:01

LaKraven


1 Answers

There is no such keyword. You can trivially change the visibility of a property by redeclaring it with a new visibility level. But there is nothing analogous for a method.

like image 188
David Heffernan Avatar answered Feb 19 '23 07:02

David Heffernan