[Pas2js] Testing generics

Mattias Gaertner nc-gaertnma at netcologne.de
Fri Oct 25 11:25:05 CEST 2019


On Thu, 24 Oct 2019 07:48:17 -0700 (MST)
warleyalex via Pas2js <pas2js at lists.freepascal.org> wrote:

> //--------
> type
>   generic TKeyValue<T> = class
>   private
>     FKey: string;
>     FValue: T;
>     procedure SetKey(const Value: string);
>     procedure SetValue(const Value: T);
>   public
>     property Key : string read FKey write SetKey;
>     property Value : T read FValue write SetValue;
>   end;
> 
>   { TKeyValue<t> }
> 
>   generic procedure TKeyValue<T>.SetKey(const Value: string);
>   begin
>     FKey := Value;
>   end;
> 
>   generic procedure TKeyValue<T>.SetValue(const Value: T);
>   begin
>     FValue := Value;
>   end;    
> 
> (...)
> var
>   KV : specialize TKeyValue<Integer>;
> begin
>   KV:= specialize TKeyValue<Integer>.Create;
>   KV.Key := 'FPC';
>   KV.Value := 2;
>   writeln('key=', KV.key, ' value=', KV.value);
> end;
> //---------------------------------------------------------
> This code compiler, but when I run, it outputs this:
> 
> Uncaught TypeError: o.AfterConstruction is not a function   --->
> rtl.js:295

It works here. Are you sure this is the complete example?
AfterConstruction is a method of TObject.

Mattias


More information about the Pas2js mailing list