### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: Constant dynamic array vs Constant set  (Read 527 times)

#### simsee

• Full Member
• Posts: 118
##### Constant dynamic array vs Constant set
« on: December 28, 2022, 08:28:56 pm »
A little doubt. Forgive the banality of the question. In ObjFpc mode, a constant dynamic array and a constant set are declared in the same way. So, how does the compiler interpret the following declaration?

Code: Pascal  [Select][+][-]
1. program Project1;
2. {\$mode ObjFpc}
3. const
4.   C=[1,2,3];
5.
6. begin
7. end.

Is C a constant dynamic array or a constant set?

Thank you.

#### howardpc

• Hero Member
• Posts: 4117
##### Re: Constant dynamic array vs Constant set
« Reply #1 on: December 28, 2022, 09:05:35 pm »
C is a set.
This is a classic set constructor syntax.
For instance:
Code: Pascal  [Select][+][-]
1. program project1;
2.
3. {\$mode ObjFpc}
4. {\$apptype console}
5. const
6.   C=[1,2,3];
7.
8.   D: array of Byte = (1,2,3);
9.
10. var
11.   i: Byte;
12.
13. begin
14.   for i in C do
15.     Write(i, ' ');
16.   WriteLn;
17.   for i in D do
18.     Write(i, ' ');
20. end.
« Last Edit: December 28, 2022, 09:10:01 pm by howardpc »

#### simsee

• Full Member
• Posts: 118
##### Re: Constant dynamic array vs Constant set
« Reply #2 on: December 28, 2022, 10:03:01 pm »
Thanks Howard. I knew what you tell me. My doubt arises with the new features introduced with fpc 3.2.0:

https://wiki.freepascal.org/FPC_New_Features_3.2.0#Dynamic_Array_constants_and_variable_initialization

https://lists.freepascal.org/pipermail/fpc-pascal/2018-May/053892.html

From that version it is possible to write:

Code: Pascal  [Select][+][-]
1. var
2.     t: array of LongInt;
3. begin
4.     t := [1, 2, 3, 4];
5. end.

Additionally, the following is possible in Delphi mode:

Code: Pascal  [Select][+][-]
1. const
2.     Test1: array of LongInt = [1, 2, 3];

Thus an enumeration inside of square brackets is no longer just the 'constructor' of a set, but also a 'constructor' of a constant dynamic array.

This is where my doubt comes from.
« Last Edit: December 28, 2022, 10:12:44 pm by simsee »

#### egsuh

• Hero Member
• Posts: 1094
##### Re: Constant dynamic array vs Constant set
« Reply #3 on: December 29, 2022, 01:05:18 am »
Set seems default unless defined otherwise.

#### howardpc

• Hero Member
• Posts: 4117
##### Re: Constant dynamic array vs Constant set
« Reply #4 on: December 29, 2022, 08:53:26 am »
Thus an enumeration inside of square brackets is no longer just the 'constructor' of a set, but also a 'constructor' of a constant dynamic array.
Although the syntax is identical, there is no ambiguity, since in both the examples you cite the variable t and the const Test1 are declared as being non-set types.

#### PascalDragon

• Hero Member
• Posts: 4963
• Compiler Developer
##### Re: Constant dynamic array vs Constant set
« Reply #5 on: December 29, 2022, 03:28:19 pm »
Code: Pascal  [Select][+][-]
1. program Project1;
2. {\$mode ObjFpc}
3. const
4.   C=[1,2,3];
5.
6. begin
7. end.

Is C a constant dynamic array or a constant set?

In the explicit example you provided C is always a constant set for two reasons:
• in non-Delphi modes array constructors in const or var sections are always denoted with ( … ), not [ … ]
• untyped array constants do not exist