Records do not offer the same possibilities as classes do. This reflects on the possibilities when
creating record helpers. Below the restrictions on record helpers are enumerated:
- A record helper cannot be used to extend a class. The following will fail:
TTestHelper = record helper for TObject
end;
- Inside a helper’s declaration the methods/fields of the extended record can’t be accessed in
e.g. a property definition. They can be accessed in the implementation, of course. This means
that the following will not compile:
TTest = record
Test: Integer;
end;
TTestHelper = record helper for TTest
property AccessTest: Integer read Test;
end;
- Record helpers can only access public fields (in case an extended record with visibility
specifiers is used).
- Inheritance of record helpers is only allowed in ObjFPC mode; In Delphi mode, it is not
allowed.
- Record helpers can only descend from other record helpers, not from class helpers.
- Unlike class helpers, a descendent record helper must extend the same record
type.
- In Delphi mode, it is not possible to call the extended record’s method using inherited. It is
possible to do so in ObjFPC mode. The following code needs ObjFPC mode to
compile:
type
TTest = record
function Test(aRecurse: Boolean): Integer;
end;
TTestHelper = record helper for TTest
function Test(aRecurse: Boolean): Integer;
end;
function TTest.Test(aRecurse: Boolean): Integer;
begin
Result := 1;
end;
function TTestHelper.Test(aRecurse: Boolean): Integer;
begin
if aRecurse then
Result := inherited Test(False)
else
Result := 2;
end;