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

1

10

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)?
Emf answered 20/9, 2014 at 16:5 Comment(0)
R
12

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.

Ruffianism answered 20/9, 2014 at 16:32 Comment(9)
FWIW, the derived object helper can be a helper for a derived class as well. IOW, this also works (compiles and produces expected result): TObjectHelperAgain = class helper(TObjectHelper) for TList. Just tried it. Of course I would call it TListHelper, then.Oak
@rudy presumably the compiler would check that the helpees are related by inheritance in the obvious way.Ruffianism
But TStreamHelper = class helper(TListHelper) for TStream won't compile. So it must be the same or a derived class that is being helped, and TSTream does not derive from TList.Oak
Ok, I wrote the above comment before I saw yours. <g>Oak
The help text implies the ancestor list is valid for record helpers as well, but it is not.Stenopetalous
@LU the docs do also say The ancestor list is optional. It can be specified only for class helper.Ruffianism
Thanks! I'm accepting your answer as solution, but for sake of completeness could you please add some input on list thing, which implies multiple identifiers as ancestors. If any, ofc.Emf
It's always just a single ancestor I think. I don't think you can add support for interfaces!Ruffianism
Yes, if you attempt to add more than one item in the list you get [dcc32 Error]: E2029 ')' expected but ',' foundRuffianism

© 2022 - 2024 — McMap. All rights reserved.