Kromster
Kromster

Reputation: 7397

Do I need to add "inherited" line into record constructors?

Modern Delphi allows constructors for records. I have the following code:

{ TKMRect }
constructor TKMRect.Create(aPoint: TKMPoint);
begin
  inherited; // <<- Do I need to add this line ?

  Left := aPoint.X;
  Top := aPoint.Y;
  Right := aPoint.X;
  Bottom := aPoint.Y;
end;

My question is - do I need to add inherited line in my records constructors? And why?

Upvotes: 10

Views: 185

Answers (1)

David Heffernan
David Heffernan

Reputation: 612854

No you don't need to do this because records don't support inheritance and so inherited is a no-op in this context.

FWIW I regard record constructors as an anti pattern. It makes it hard for the reader at the call site to distinguish between value type and reference type. I personally use static class functions named New that return a new value for this purpose. You can argue over whether a different name is better, but it doesn't matter so long is it isn't Create.

Upvotes: 10

Related Questions