Search code examples
delphiclass-helpers

What the purpose of `ancestor list` in the class helper syntax? Where it is documented? Are there any usage example(s)?


All of the documentation versions, including the one most up to date gives the following class/record helper syntax:

type
   identifierName = class|record helper [(ancestor list)] for TypeIdentifierName
     memberList
   end;

And it only explains what...

The ancestor list is optional. It can be specified only for class helper.

... and goes into dire details no further. An usage examples in the rest of the documentation topic merely exploit the fact what ancestor list is optional. All of EMBA's code I've seen as well as all of third-party code does not use this ancestor list part.

So, my questions are outlined in the title:

  • What the purpose of ancestor list in the class helper syntax?
  • Where it is documented?
  • Are there any usage example(s)?

Solution

  • It allows for inheritance of helpers:

    {$APPTYPE CONSOLE}
    
    type
      TObjectHelper = class helper for TObject
        procedure Foo;
      end;
    
      TObjectHelperAgain = class helper(TObjectHelper) for TObject
        procedure Bar;
      end;
    
    procedure TObjectHelper.Foo;
    begin
      Writeln('Foo');
    end;
    
    procedure TObjectHelperAgain.Bar;
    begin
      Writeln('Bar');
    end;
    
    begin
      with TObject.Create do
      begin
        Foo;
        Bar;
      end;
    end.
    

    This is one way to work around the limitation that there can be only a single helper active at any particular code location.

    So far as I can tell, there is no documentation for the ancestor list.