I have a problem with helper re-declaration in Delphi.
HelperDecl.pas
unit HelperDecl;
interface
type
TCustomField = Word;
TCustomFieldHelper = record helper for TCustomField
public
procedure SampleMethod();
end;
implementation
procedure TCustomFieldHelper.SampleMethod();
begin
end;
end.
ScopeTest.pas
unit ScopeTest;
interface
uses HelperDecl;
type
rec = record
art: TCustomField;
end;
implementation
uses System.SysUtils;
procedure DoScopeTest();
var
a: TCustomField;
r: rec;
begin
a := r.art;
r.art.SampleMethod(); //Here has the compiler no problems
a.SampleMethod(); //Undeclared identifier 'SampleMethod'
end;
end.
But I have defined a helper only for my local data type (yes, it is derived from Word)! The helper in SysUtils
is the helper for Word
, not for my custom data type! Hands off my data type!
When I move uses System.SysUtils;
before uses HelperDecl;
then it works. But I would like to have an arbitrary units usage order.
The problem is that TCustomField
and Word
is the same type and it is not possible to have two record helpers for the same type.
If you instead let TCustomField
by a distinct type, it will work:
type
TCustomField = type Word;
Delphi only supports single helper. When you have multiple helpers in scope, helper from nearest scope is used.
You can define and associate multiple helpers with a single type. However, only zero or one helper applies in any specific location in source code. The helper defined in the nearest scope will apply. Class or record helper scope is determined in the normal Delphi fashion (for example, right to left in the unit's uses clause).
Class and Record Helpers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With