Bi-dimensional array in Pas2JS

Top 

Example code : Bi-dimensional array in Pas2JS

type

  TDayTable = array[1..12of Integer;

 

procedure TForm1.W3Button2Click(Sender: TObject);

const

  LeapMonthDays: TDayTable = (312931303130313130313031);

const

  NormalMonthDays: TDayTable = (312831303130313130313031);

const

  MonthDays: array[boolean] of TDayTable = (NormalMonthDays, LeapMonthDays);

const

  MonthDays1: array[boolean] of TDayTable =

    [[312831303130313130313031],

    [312931303130313130313031]];

begin

  WriteLn(MonthDays[false2]);

    // LeapMonthDays = false and Month = February --> 28 days

  WriteLn(MonthDays[true2]);

    // LeapMonthDays = true  and Month = February --> 29 days

 

  WriteLn(MonthDays1[false2]);

    // LeapMonthDays = false and Month = February --> 28 days

  WriteLn(MonthDays1[true2]);

    // LeapMonthDays = true  and Month = February --> 29 days

end;

Result is:

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

28

29

28

29

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

 

JS output:

var Days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"];

 

function W3Button2Click(Self, Sender$5) {

      var MonthDays1 = [[31,28,31,30,31,30,31,31,30,31,30,31],[31,29,31,30,31,30,31,31,30,31,30,31]],

          MonthDays = [[31,28,31,30,31,30,31,31,30,31,30,31],[31,29,31,30,31,30,31,31,30,31,30,31]],

          NormalMonthDays = [31,28,31,30,31,30,31,31,30,31,30,31],

          LeapMonthDays = [31,29,31,30,31,30,31,31,30,31,30,31];

      WriteLn(28);

      WriteLn(29);

      WriteLn(28);

      WriteLn(29);

   }

 

Note: Const MonthDays and MonthDays1 are equivalents.

 

Delimiter for array constants is [ ]

The ( ) was supported only for non-nested arrays (when the type is known).

The ( ) constant array syntax is only supported for constant definitions, while [ ] is supported everywhere.

The reason behind using [ ] is to support array constants outside constant declarations, to initialize an array variable,

f.i, and also to allow type inference.