[fpc-pascal] Question on type helpers

Bart bartjunk64 at gmail.com
Sat Dec 31 16:28:17 CET 2016


Hi,

Why is it that type helpers must be declared in the Interface section
of a unit and cannot be private in Implementation section?

Consider this (useless) example:

unit thtest;

{$mode objfpc}{$H+}
{$modeswitch typehelpers}

interface

type
  TSomeType = record end;
  TA = Array of TSomeType;

  { TAHelper }

  TAHelper = type helper for TA
    constructor Create(A: TA);
    procedure DoIt;
  end;

procedure X;

implementation

procedure X;
var
  A: TA;
begin
  A.DoIt;
end;

{ TAHelper }

constructor TAHelper.Create(A: TA);
begin
  Self := A;
end;

procedure TAHelper.DoIt;
begin
  ;
end;

end.

This compiles fine.
But when you move the declaration of TAHelper to the implementation
section, then you get a compiler error:

thtest.pp(21,5) Fatal: Syntax error, "CREATE" expected but "DOIT" found
Line 21 is
  A.DoIt;

I'm using fpc 3.0.0 (32-bit) on Windows.

Bart



More information about the fpc-pascal mailing list