[fpc-pascal] Question on type helpers

Bart bartjunk64 at gmail.com
Sat Dec 31 22:41:15 CET 2016


On 12/31/16, Sven Barth <pascaldragon at googlemail.com> wrote:

> You did put the helper declaration before the implementation of X, right?

Oops!

> If you still have a problem, please open a bug report with an example
> (cause that should definitely work).

This is from my oiginal code.
I stripped away anything that did not influence this (lots of methods,
uses clause for sysutils,class,lazutf8).

======================================
unit thtest;

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

interface

type
  TStringArray = array of String;

  { TStringTable }

  TStringTable = class
  private
    FRows: array of TStringArray;
  protected
  public
    procedure DeleteCol(Index: Integer);
    function InsertCol(Index: Integer; Values: array of String): Integer;
  end;

implementation

  { TStringArrayHelper }
type
  TStringArrayHelper = type helper for TStringArray
    constructor Create(AValue: TStringArray); overload;
    function Insert(Index: Integer; const S: String): Integer;
    procedure Delete(Index: Integer);
  end;

{ TStringArrayHelper }

constructor TStringArrayHelper.Create(AValue: TStringArray);
begin
  Self := AValue;
end;

function TStringArrayHelper.Insert(Index: Integer; const S: String): Integer;
var
  i: Integer;
begin
  SetLength(Self, Length(Self)+1);
  for i := High(Self) downto (Index + 1) do
    Self[i] := Self[i-1];
  Self[Index] := S;
  Result := Index;
end;

procedure TStringArrayHelper.Delete(Index: Integer);
var
  i: Integer;
begin
  for i := Index to (High(Self) - 1) do
    Self[i] := Self[i+1];
  SetLength(Self, Length(Self) - 1);
end;

{ TStringTable }

function TStringTable.InsertCol(Index: Integer; Values: array of
String): Integer;
var
  i: Integer;
begin
  FRows[0].Insert(Index, '');
  //for i := Low(FRows) to High(FRows) do
  begin
    //if (i <= High(Values)) then
      FRows[i].Insert(Index, Values[i])
    //else
    //  FRows[i].Insert(Index, '');
  end;
  Result := Index;
end;

procedure TStringTable.DeleteCol(Index: Integer);
var
  i: Integer;
begin
  FRows[0].Delete(Index);
end;

end.
======================================

The declaration of the helper is before any location is is used as far
as I can see.

thtest.pp(80,12) Fatal: Syntax error, "CREATE" expected but "DELETE" found
Line 80 is:
  FRows[0].Delete(Index);

Why does the compile NOT like FRows[0].Delete(), but does not care
about the earlier
(line 69) FRows[i].Insert(Index, Values[i]) ??

Even stranger (to me):

If you uncomment
  //FRows[0].Insert(Index, '');
at line 65, the compiler will see the error at the above mentioned line 69:

thtest.pp(69,16) Fatal: Syntax error, "CREATE" expected but "INSERT" found

But why does it not fail then at the uncommented line that also calls
the Insert method?

NB. These are my compiler options:
-MObjFPC -Scghi -Cirot -g -gl -gh -l -vewnhibq -Filib\i386-win32 -Fu.
-FUlib\i386-win32 -FcUTF8

Bart



More information about the fpc-pascal mailing list