[fpc-pascal] Generic specialization using $mode objfpc

Kiên Nguyễn Tiến Trung kc97ble at gmail.com
Fri Oct 31 04:01:42 CET 2014


AFAIK, you must create a new type:

type
    TMyGenericInt = specialize TMyGeneric<Integer>;
var
    VMyGenericInt: TMyGenericInt;

begin
    VMyGenericInt:= TMyGenericInt.Create;
    VMyGenericInt.Value := 2014;
    VMyGenericInt.Free;
end.

2014-10-31 2:14 GMT+07:00 silvioprog <silvioprog at gmail.com>:

> 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
>
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20141031/ad0034dc/attachment.html>


More information about the fpc-pascal mailing list