[Pas2js] array literals
Mattias Gaertner
nc-gaertnma at netcologne.de
Sat Feb 16 09:45:47 CET 2019
On Fri, 15 Feb 2019 19:39:21 -0700 (MST)
warleyalex via Pas2js <pas2js at lists.freepascal.org> wrote:
Tests of Array literals
Note: array literals work since a long time.
> ***********************************************************
> Test I
> ==============================================
> const PId = 3.141592653589793 * 2; // compiler generates possible
> dead code
> const LETTERS = ['A'..'Z', 'a'..'z']; // it's OK
> console.log(PId);
>
> Note:-> the pas2js emits this JS
> this.PId = 3.141592653589793 * 2; ---> this is probably a dead code
> window.console.log(6.2831853071795862);
First of all 2*pi is usually called tau.
And yes, the WPO currently does not check if all references of a
constant are replaced by the literal.
Note: In this particular case it would be shorter to use this.PId
instead of the literal.
> ==============================================
> Test II
> ==============================================
> const
> a1 : array [0..2] of String = ('zero', 'one', 'two'); //--> hum it
> uses parenteses delimiter, it's OK.
>
> var
> a2 : array [0..2] of String;
>
> console.log(a1);
> a2 := ['zero', 'one', 'two'];
> console.log(a2);
>
> Note: it emits this:
> this.a1 = ["zero","one","two"]; // OK initialization
> $mod.a2 = ["zero","one","two"];
> window.console.log($mod.a1.slice(0)); // slice(0) allows you to
> return an array of the existing array you're referencing, a simple
> window.console.log($mod.a1) is enough a think;
That's because the log function parameter was not 'const'. I changed
that now.
>[...]
> ==============================================
> test V
> ==============================================
> type TEnum = (en1, en2, en3);
>
> const ace : array [TEnum] of String = ('one', 'two', 'three');
> const ac2 = ace; ---> project1.lpr(57,15) Error: Constant
> expression expected
This is FPC/Delphi compatible.
They do not even support it when using {$writeableconst off}
> ==============================================
> test VI
> ==============================================
> type TEnum = (en1, en2, en3);
>
> const ace : array [TEnum] of String = ('one', 'two', 'three');
> //const ac2 = ace;
>
> var j : TEnum;
>
> type TEnum2 = (en1, en2, en3); //----> I think this is a bug,
> project1.lpr(61,19) Error: Duplicate identifier "en1" at
> project1.lpr(54,20)
What exactly is a bug?
Maybe you meant {$scopedenums on} ?
> ==============================================
> test VII
> ==============================================
> type TEnum = (en1, en2, en3);
>
> const ace : array [TEnum] of String = ('one', 'two', 'three');
> //const ac2 = ace;
>
> var j : TEnum;
>
> type TEnum2 = (en01, en02, en03);
> type TEnumArray = array [TEnum2] of string;
>
> const acb : TEnumArray = ('one', 'two', 'three');
> const ac21 : TEnumArray = acb; //--> it compiles as expected!
Note that FPC/Delphi do not allow this.
pas2js gives an error when using ac21. It should give an error right
away, or it should be implemented.
>[...]
> ==============================================
> test VIII
> ==============================================
> const
> X: array [0..1, 0..2] of Integer = ([1, 2, 3], [4, 5, 6]);
>
> console.log(X[0,0]);
>
> for w := Low(X) to High(X) do
> for z := Low(X[w]) to High(X[w]) do // ---> we got "Error: Wrong
> number of parameters for array"
> console.log(IntToStr(w)+', '+IntToStr(z)+' : '+IntToStr(X[w,
> z]));
Please create a bug report.
>[...]
Mattias
More information about the Pas2js
mailing list