[fpc-pascal] Generic<T> compiler error
    Marcos Douglas B. Santos 
    md at delfire.net
       
    Mon Oct 23 04:14:07 CEST 2017
    
    
  
Hi,
In Pascal we can do this:
type
  TXStream = TStream;
Now, TXStream is just an alias to TStream.
How do the same with generic classes, using mode delphi?
  TBar<T> = TFoo<T>;
Compile Project, Target: C:\temp\project1.exe: Exit code 1, Errors: 4
project1.lpr(25,18) Error: Identifier not found "T"
project1.lpr(25,19) Error: Type identifier expected
project1.lpr(25,20) Error: Error in type definition
project1.lpr(27,1) Error: This type cannot be a generic
My ENV is:
Lazarus 1.9.0 r56056M
FPC 3.1.1 r37505
i386-win32-win32/win64
Please, see an example below:
=== BEGIN ===
program Project1;
{$mode delphi}
uses
  Classes;
type
  IFoo<T> = interface
  ['{5CA4AEC4-3875-4639-9975-AC2C201E0A98}']
    function Value: T;
  end;
  TFoo<T> = class(TInterfacedObject, IFoo<T>)
  private
    FValue: T;
  public
    constructor Create(Value: T);
    class function New(Value: T): IFoo<T>;
    function Value: T;
  end;
  TXStream = TStream;  // <<<< OK
  TBar<T> = TFoo<T>; // <<<< how solve this?
constructor TFoo<T>.Create(Value: T);
begin
  inherited Create;
  FValue := Value;
end;
class function TFoo<T>.New(Value: T): IFoo<T>;
begin
  Result := Create(Value);
end;
function TFoo<T>.Value: T;
begin
  Result := FValue;
end;
begin
  Writeln(TFoo<Integer>.New(1).Value);
  Readln;
end.
=== END ===
    
    
More information about the fpc-pascal
mailing list