[fpc-pascal] Generic specialization using $mode objfpc

silvioprog silvioprog at gmail.com
Thu Oct 30 20:14:50 CET 2014


Hello,

How to use this code below in mode objfpc?:

  TMyGeneric<T> = class(TObject)
  private
    FValue: T;
  public
    property Value: T read FValue write FValue;
  end;

...

var
  VMyGenericStr: TMyGeneric<Integer>;
  VMyGenericInt: TMyGeneric<string>;
begin
  VMyGenericStr := TMyGeneric<Integer>.Create;
  VMyGenericStr.Value := 2014;
  VMyGenericStr.Free;

  VMyGenericInt := TMyGeneric<string>.Create;
  VMyGenericInt.Value := 'Delphi Generics';
  VMyGenericInt.Free;
end;

When I try:

  generic TMyGeneric<T> = class(TObject)
  private
    FValue: T;
  public
    property Value: T read FValue write FValue;
  end;

...

var
  VMyGenericStr: specialize TMyGeneric<Integer>;
  VMyGenericInt: specialize TMyGeneric<string>;
begin
  VMyGenericStr := TMyGeneric<Integer>.Create;
  VMyGenericStr.Value := 2014;
  VMyGenericStr.Free;

  VMyGenericInt := TMyGeneric<string>.Create;
  VMyGenericInt.Value := 'Delphi Generics';
  VMyGenericInt.Free;
end;

I got:

Error: Illegal expression
Error: Operator is not overloaded: "Class Of TMyGeneric$1" < "LongInt"
Fatal: Syntax error, ";" expected but "identifier CREATE" found

Thank you!

ps: FPC from trunk.

-- 
Silvio Clécio
My public projects - github.com/silvioprog
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20141030/3c5dcbf8/attachment.html>


More information about the fpc-pascal mailing list