Search code examples
delphihelperdelphi-xe5

Delphi helpers scope


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.


Solution

  • 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;