[Pas2js] Initialize dynamic array feature

warleyalex warleyalex at yahoo.com.br
Thu Feb 15 21:56:31 CET 2018


It would be nice if we could do something like this, but we can not.
-------------------------------------------------------------------
var
  dynamicArray  : array of String;
  dynamicArray1 : array of String = ('one', 'two', 'three');

begin
   dynamicArray := ('one', 'two', 'three');  // we can not do this!
end;
-------------------------------------------------------------------

Workaround I've came across these handy functions:

function CloneArray(const A: array of string): TStringDynArray; overload;
var
  Idx: Integer;
begin
  SetLength(Result, Length(A));
  for Idx := Low(A) to High(A) do
    Result[Idx - Low(A)] := A[Idx];
end;

function CloneArray(const A: array of integer): TIntegerDynArray; overload;
var
  Idx: Integer;
begin
  SetLength(Result, Length(A));
  for Idx := Low(A) to High(A) do
    Result[Idx - Low(A)] := A[Idx];
end; 

------------------------------------------------------
var
  dynamicArray3: TStringDynArray;
  dynamicArray4: TIntegerDynArray;  
begin
   dynamicArray3 := CloneArray(['one', 'two', 'three']);
   dynamicArray3[3] := 'teste';
   dynamicArray4 := CloneArray([1,2,3,4,5]);
end;
------------------------------------------------------
Another thing: #about TJSArray is when we declare, e.g.

var
  dynamicArray2: TJSArray;

the pas2js compiler initialize the empty array with nil 
this.dynamicArray2 = null;

It would be handy if would initialize with [] instead of nil
this.dynamicArray2 = [];
------------------------------------------------------



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


More information about the Pas2js mailing list