Recent

Author Topic: How to initialize the array  (Read 16024 times)

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How to initialize the array
« Reply #15 on: December 29, 2020, 12:46:43 pm »
Just for curiosity: why this syntax difference between Delphi and ObjFPC modes?

Delphi devs elected parens "()" for reasons of their own*, while FPC devs preferred to keep a consistent notation for arrays everywhere. That's all.


* Probably to make it different from that of sets.
« Last Edit: December 29, 2020, 12:51:07 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

simone

  • Hero Member
  • *****
  • Posts: 573
Re: How to initialize the array
« Reply #16 on: December 29, 2020, 01:27:21 pm »
I'm not sure to understand, because starting from FPC 3.2.0, it is possible to write the following code:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2. var
  3.    Arr2: array of String = ('xxx', 'yyy', 'zzz'); //initialization of dynamic array in objfpc mode. in delphi mode "[...]" instead of "(...)"
  4.    Arr3 : array of String;
  5.  
  6. begin
  7.   Arr3:=['aaa','bbb','ccc']; //array constructor
  8. end.  

So, in Delphi mode: "[...]" are used for initialization and constructors, whereas in Objfpc mode "(...)" are used for initialization,  "[...]" for constructors.

Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: How to initialize the array
« Reply #17 on: December 29, 2020, 04:47:25 pm »
It was decided that FPC stays consistent with constants for static arrays which are declared using ( ... ) since the introduction of such constants in TP. Delphi uses it like this for static array constants as well, but for dynamic arrays they went a different way, go figure. ::)

However for e.g. open array constructors [ ... ] is already used, thus using [ ... ] for dynamic array constructors is a logical extension (and Delphi did it this way as well).

furious programming

  • Hero Member
  • *****
  • Posts: 858
Re: How to initialize the array
« Reply #18 on: December 29, 2020, 08:53:09 pm »
Since 3.2.0 I'd suggest to use the inline array constructor [ ... ] instead of TSomeArrayType.Create, because the former does not require a named type.

Fine, but it is working for open arrays:

Code: Pascal  [Select][+][-]
  1. var
  2.   Foo: array of Integer;
  3. begin
  4.   Foo := [1, 2, 3]; // valid construction

but not for typed dynamic arrays, such as SysUtils.TIntegerArray:

Code: Pascal  [Select][+][-]
  1. uses
  2.   SysUtils;
  3. var
  4.   Foo: TIntegerArray;
  5. begin
  6.   Foo := [1, 2, 3]; // invalid construction

it causes compilation error:

Code: Pascal  [Select][+][-]
  1. // project1.lpr(?,?) Error: Incompatible types: got "{Array Of Const/Constant Open} Array of ShortInt" expected "IntegerArray"
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

bytebites

  • Hero Member
  • *****
  • Posts: 640
Re: How to initialize the array
« Reply #19 on: December 30, 2020, 07:29:09 am »
TIntegerArray is not dynamic array.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: How to initialize the array
« Reply #20 on: December 30, 2020, 11:13:59 am »
TIntegerArray is not dynamic array.

Correct, it's declared like this:

Code: Pascal  [Select][+][-]
  1.        { array types }
  2. {$ifdef CPU16}
  3.        IntegerArray  = array[0..(32768 div SizeOf(Integer))-2] of Integer;
  4. {$else CPU16}
  5.        IntegerArray  = array[0..$effffff] of Integer;
  6. {$endif CPU16}
  7.        TIntegerArray = IntegerArray;
  8.        PIntegerArray = ^IntegerArray;
  9.  

The Create wouldn't work there either. ;)

furious programming

  • Hero Member
  • *****
  • Posts: 858
Re: How to initialize the array
« Reply #21 on: December 30, 2020, 11:22:42 am »
TIntegerArray is not dynamic array.

LOL, I think there was cerebral death — I meant TIntegerDynArray:

Code: Pascal  [Select][+][-]
  1. uses
  2.   Types;
  3. var
  4.   Foo: TIntegerDynArray;
  5. begin
  6.   Foo := [1, 2, 3];

and of course it works perfectly fine. What a shame… sorry for that… :D
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

piola

  • Full Member
  • ***
  • Posts: 124
  • Lazarus 2.2, 64bit on Windows 8.1 x64
Re: How to initialize the array
« Reply #22 on: June 29, 2021, 10:38:57 pm »
Is there an elegant equivalent to intialize a static array, too?

Code: Pascal  [Select][+][-]
  1. type TMyArray = Array[1..10] of Integer;
  2. var foo: Array of TMyArray;
  3. begin
  4.   SetLength (foo, 1);
  5.   foo[0] := [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1]; // Incompatible types: got "{Array Of Const/Constant Open} Array of ShortInt" expected "TMyArray"
  6.   foo[0] := TMyArray.Create(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1); // illegal qualifier "Create"
  7. end;
  8.  
« Last Edit: June 29, 2021, 10:41:59 pm by piola »

speter

  • Sr. Member
  • ****
  • Posts: 349
Re: How to initialize the array
« Reply #23 on: June 30, 2021, 09:06:08 am »
Do the following, after setlength()

Code: Pascal  [Select][+][-]
  1. foo[0] := default(TMyArray);

Note that this initialises the array's data to 0.

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: How to initialize the array
« Reply #24 on: June 30, 2021, 09:08:44 am »
Is there an elegant equivalent to intialize a static array, too?

Code: Pascal  [Select][+][-]
  1. type TMyArray = Array[1..10] of Integer;
  2. var foo: Array of TMyArray;
  3. begin
  4.   SetLength (foo, 1);
  5.   foo[0] := [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1]; // Incompatible types: got "{Array Of Const/Constant Open} Array of ShortInt" expected "TMyArray"
  6.   foo[0] := TMyArray.Create(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1); // illegal qualifier "Create"
  7. end;
  8.  

In FPC trunk (3.3.1) you can use an inline array constructor as well (so your first assignment will work). Though in my opinion it's more elegant to use the following if you can't use Default (which will initialize to 0):

Code: Pascal  [Select][+][-]
  1. type
  2.   TMyArray = array[1..10] of Integer;
  3. var
  4.   a: TMyArray;
  5. begin
  6.   FillDWord(a, Length(a), DWord(-1));
  7. end.

Kays

  • Hero Member
  • *****
  • Posts: 575
  • Whasup!?
    • KaiBurghardt.de
Re: How to initialize the array
« Reply #25 on: June 30, 2021, 10:37:44 am »
[…] Though in my opinion it's more elegant to use the following if you can't use Default (which will initialize to 0): […]
The most elegant solution is probably as defined in Extended Pascal, ISO 10206:
Code: Pascal  [Select][+][-]
  1. program arrayInitialization;
  2. type
  3.         integerSequence = array[1..42] of integer;
  4. var
  5.         { demonstration of the syntax: }
  6.         a: integerSequence value [1: 7; 42: 6; otherwise -1];
  7.         { everything a single value: }
  8.         i: integerSequence value [otherwise -1];
  9. begin
  10. end.
The GPC can already compile that and it works as expected. The FPC is unfortunately lagging behind that. :(
Yours Sincerely
Kai Burghardt

piola

  • Full Member
  • ***
  • Posts: 124
  • Lazarus 2.2, 64bit on Windows 8.1 x64
Re: How to initialize the array
« Reply #26 on: June 30, 2021, 12:20:22 pm »
Thank you! I'll probably switch to trunk then  :)

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: How to initialize the array
« Reply #27 on: June 30, 2021, 01:59:12 pm »
The GPC can already compile that and it works as expected. The FPC is unfortunately lagging behind that. :(

Support for Extended Pascal while desired is low priority. The majority of users desires features for which Extended Pascal does not have an answer (namely things like Extended RTTI and anonymous functions). So if you want something pushed, then provide patches for it. Though even then those features would normally only be available in ExtendedPascal mode and only after consideration maybe in other modes (features like radix numbers could possibly be exceptions however).

krzynio

  • Jr. Member
  • **
  • Posts: 99
    • Krzynio's home page
Re: How to initialize the array
« Reply #28 on: March 03, 2022, 09:25:21 am »
Hi!
Lazarus 2.2.0, FPC: 3.2.2
Can someone help me and explain why the first line works fine but the second one generates an error  "Incompatible types: got "Constant String" expected "Char"
Code: Pascal  [Select][+][-]
  1. a: array of AnsiChar = ('r', 'a', 'f');  // it works
  2. PLChars: array of AnsiChar = ('ą', 'ć', 'ę', 'ł', 'ń', 'ó', 'ś', 'ż', 'ź', 'Ą', 'Ć', 'Ę', 'Ł', 'Ń', 'Ó', 'Ś', 'Ż', 'Ź');     // it doesn't work
  3.  
Ubuntu 23.10 x64, / Windows 11 PL - latest updates
Lazarus 2.2.6, FPC 3.2.2

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: How to initialize the array
« Reply #29 on: March 03, 2022, 09:30:49 am »
Because Char is single-byte?
have you tried WideChar?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018