[Pas2js] Testing generics

warleyalex warleyalex at yahoo.com.br
Sat Oct 26 14:03:55 CEST 2019


I had a new TOject definition class in somewhere that was causing this
"Uncaught TypeError: o.AfterConstruction is not a function   ---> 
rtl.js:295". I deleted this TObject definition and the run as expected.

(*
type
  TObject = class
  public
    constructor Create;
  end;  
*)
------------------
Another thing is:
I would like to create a procedure, e.g. a list of people, without the use
of generics; and another with the use of generics.  

---// CODE -----------------
uses
  JS, Classes, SysUtils, Web, Types, Math, TypInfo,
  generics.collections, generics.defaults, generics.strings;

  {$mode objfpc}

type
  TPerson = class
  private
    FNome: String;
    procedure SetNome(const Value: String);
  public
    property Nome: String read FNome write SetNome;
  end;

{ TPerson }
procedure TPerson.SetNome(const Value: String);
begin
  FNome := Value;
end;

procedure PersonWithoutGenerics;
var
  LListaPessoa: TList; //--> Error: Generics without specialization cannot
be used as a type for a variable
begin

end;

procedure PersonWithGenerics;
var
  LListaPessoas: specialize TList<TPerson>;
begin

//-----------------
The problem is that the compiler shows:

Error: Generics without specialization cannot be used as a type for a
variable

hum, the Generics.Collections unit, we have this definiton: TList<T> =
class(TCustomList<T>)
and the classes unit this one: TList = class(TObject)  

if I move the "Classes" unit after the "Generics.Collections" at the uses
clauses the code compile without errors.




--
Sent from: http://pas2js.38893.n8.nabble.com/


More information about the Pas2js mailing list