Dynamic array instance using New in SmartMS

Top 

Example code : Dynamic array instance using New in SmartMS

// Defining an array

var

  a, b : array of Integer;

 

begin

// Instantiate an array

  a := new Integer[5];

  b := a;

  a[1] := 10;

  WriteLn( b[1] );  // 10

 

 

  a.SetLength(10);

  a[1] := 20;

  WriteLn( b[1] );  // 20

Result is: 10 and 20

------------------------------------------------------------------------

 

In other words, in SmartMS, if you want a new dynamic array instance, you use "new", if want to make a copy of a dynamic array, you have to use Copy() method, if you want to resize, you use SetLength() method. See the similar example at Copy() method.

 

JS output:

      var a$54 = [];

      var b$18 = [];

      var i$5 = 0;

      a$54 = [0,0,0,0,0];

      b$18 = a$54;

      $DIdxW(a$54,1,10,"");

      WriteLn($DIdxR(b$18,1,""));

      

      $ArraySetLen(a$54,10,0);

      $DIdxW(a$54,1,20,"");

      WriteLn($DIdxR(b$18,1,""));

 

function $DIdxW(a,i,v,z) {

   if (i<0) throw Exception.Create($New(Exception),"Lower bound exceeded! Index "+i.toString()+z);

   if (i>a.length) throw Exception.Create($New(Exception),"Upper bound exceeded! Index "+i.toString()+z);

   a[i]=v;

};

 

function $DIdxR(a,i,z) {

   if (i<0) throw Exception.Create($New(Exception),"Lower bound exceeded! Index "+i.toString()+z);

   if (i>a.length) throw Exception.Create($New(Exception),"Upper bound exceeded! Index "+i.toString()+z);

   return a[i];

};

 

function $ArraySetLen(a,n,v) {

   var o=a.length;

   if (o==n) return;

   if (o>n) a.length=n; else for (;o<n;o++) a.push(v);

};