Recent

Author Topic: [solved] Array Initialise  (Read 1079 times)

dbannon

  • Hero Member
  • *****
  • Posts: 2796
    • tomboy-ng, a rewrite of the classic Tomboy
[solved] Array Initialise
« on: December 12, 2022, 02:23:50 am »
This rather puzzles me, can anyone explain the differences between the following variable declarations, the first two work fine, the third fails with "plotter.pas(51,45) Fatal: Syntax error, "(" expected but "identifier COLRED" found"  -

Code: Pascal  [Select][+][-]
  1. uses  classes, sysutils, FPImage, FPCanvas, FPImgCanv, FPWritePNG;
  2.  
  3. var
  4.     // next one is OK
  5.     Integers : array of integer = (1,2,3);
  6.     // next one is OK
  7.     PlotColours : array of TfpColor = ((Red: $ffff; Green: $0000; Blue: $0000; Alpha: alphaOpaque), (Red: $0000; Green: $ffff; Blue: $0000; Alpha: alphaOpaque));
  8.     // Next one is broken
  9.     ShortPlotColours : array of TfpColor = (colRed, colGreen);
             

Things like colRed are Const, declared like this in fpcolors.inc

Code: Pascal  [Select][+][-]
  1. const
  2. ......
  3.   colRed        : TFPColor = (Red: $ffff; Green: $0000; Blue: $0000; Alpha: alphaOpaque);
  4. ......
               

This is FPC 3.2.2 on Linux. Obviously I can use the long form for my need but its very wordy ....

Davo

EDIT: typo
EDIT2 : removed 'static' from title, the array I am talking about is dynamic !
« Last Edit: December 13, 2022, 06:24:49 am by dbannon »
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Nicole

  • Hero Member
  • *****
  • Posts: 970
Re: Static Array Initialise
« Reply #1 on: December 12, 2022, 11:00:53 am »
What happens, if you declare them as types insteads of Const?

To my mind, the "Const" is the problem.

WooBean

  • Full Member
  • ***
  • Posts: 230
Re: Static Array Initialise
« Reply #2 on: December 12, 2022, 11:23:24 am »
What happens, if you declare them as types insteads of Const?

To my mind, the "Const" is the problem.
You mean var declaration in mode {$mode objFPC} which allows initializing variables just in its declaration?
"Const" and "var" have the same limitations.
It is not documented that it is possible to use previously declared constant records as statements for declaring arrays of records (except constants used for ordinal record fields values as it is in the field Alpha: alphaOpaque).
Platforms: Win7/64, Linux Mint Ulyssa/64

jan741

  • New Member
  • *
  • Posts: 15
Re: Static Array Initialise
« Reply #3 on: December 12, 2022, 11:30:11 am »
Code: Pascal  [Select][+][-]
  1. const
  2.   cint1 = 5;
  3.   cint2 : integer=5;  
  4.  
  5. ....
  6.  
  7. var
  8.   vint1 : array of integer=(cint1,5,6,7);   //<----------- compiles without problem
  9.   vint2 : array of integer=(cint2,5,6,7);   //<----------- illigal expression
  10.  
  11. begin
  12.   cint1 := 3;  //<------- error
  13.   cint2 := 3;  //<------- ok
  14. end.

cf: typed consts are just vars.
« Last Edit: December 12, 2022, 01:32:16 pm by jan741 »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Static Array Initialise
« Reply #4 on: December 12, 2022, 12:13:57 pm »
This rather puzzles me, can anyone explain the differences between the following variable declarations, the first two work fine, the third fails with "plotter.pas(51,45) Fatal: Syntax error, "(" expected but "identifier COLRED" found"
Because typed constants are not true constants and therefore cannot be used to initialize variables. This requires historical knowledge of typed constants from the days of Turbo Pascal, because Free Pascal values compatibility more than sensibility. Not necessarily a bad thing as this helps porting old Turbo Pascal code. And because there's no way to declare a true record constant, the parser expects "(" as it knows that's the only way to initialize a record var/const, i.e. by specifying and filling the fields one by one.

dbannon

  • Hero Member
  • *****
  • Posts: 2796
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Static Array Initialise
« Reply #5 on: December 12, 2022, 01:10:07 pm »
Ah, yes, that makes sense, "typed constants", yes, I know they are quite a different beast.

Thanks folks, I feel more comfortable about it now !

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Static Array Initialise
« Reply #6 on: December 12, 2022, 01:28:49 pm »
"static array initialize" is the wrong header for the question. You are using dynamic arrays that you want to initialize. Static arrays need a length declaration: x: array[0..3] of integer;

But static arrays can also be initialized:
Code: Pascal  [Select][+][-]
  1. {$ifdef mswindows}{$apptype console}{$endif}
  2. {$mode objfpc}
  3. var
  4.   x:array[0..3] of integer =(1,2,3,4);
  5. const
  6.   y:array[0..3] of integer =(5,6,7,8);
  7. begin
  8.   writeln(x[2]);
  9.   writeln(y[3]);
  10. end.
So can be const or var, but not a type with this syntax.
« Last Edit: December 12, 2022, 01:49:39 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

dbannon

  • Hero Member
  • *****
  • Posts: 2796
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Static Array Initialise
« Reply #7 on: December 13, 2022, 06:20:58 am »
OK, thats a surprisingly helpful comment Thaddy. And I thank you !

I assumed because I was telling the compiler all about what the array was to hold that it would then be static. After all, it had everything it needs to fill in that array at compile time.  But, as Thaddy says, its Dynamic. So, that opens up other possibilities -

Code: Pascal  [Select][+][-]
  1. type TFPColorArray = array of TFPColor;
  2.  
  3. var
  4.     PlotColours : TFPColorArray;
  5.  
  6. ....
  7.  
  8. PlotColours := TFPColorArray.Create(colRed, colDkGreen, colBlue, colMaroon, colMagenta);
And that compiles and runs quite happily.

I did have to experiment a bit to prove to myself that the array is Dynamic, try calling setlength() on it ....

I find it strange how the original form of initialization I tried, the one that fails with typed constants, is not documented anywhere where I can find it. I am talking about, eg -
Code: Pascal  [Select][+][-]
  1. var
  2.   x:array[0..3] of integer =(1,2,3,4);     // Static
  3.   y:array of integer =(1,2,3,4);   // dynamic

While I did not quite understand the process (I do now) its still a very convenient way to work. Is it frowned upon, not officially supported, whatever ? I'd like to add something to the wiki ....

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Static Array Initialise
« Reply #8 on: December 13, 2022, 09:47:07 am »
I find it strange how the original form of initialization I tried, the one that fails with typed constants, is not documented anywhere where I can find it. I am talking about, eg -
Code: Pascal  [Select][+][-]
  1. var
  2.   x:array[0..3] of integer =(1,2,3,4);     // Static
  3.   y:array of integer =(1,2,3,4);   // dynamic

While I did not quite understand the process (I do now) its still a very convenient way to work. Is it frowned upon, not officially supported, whatever ? I'd like to add something to the wiki ....
It was documented by Sven in his mailing list announcement
https://lists.freepascal.org/pipermail/fpc-pascal/2018-May/053892.html
but perhaps has not made it yet into the official documentation anywhere.

ASerge

  • Hero Member
  • *****
  • Posts: 2242
Re: Static Array Initialise
« Reply #9 on: December 13, 2022, 06:02:44 pm »
but perhaps has not made it yet into the official documentation anywhere.
Dynamic array constant expressions.
And isn't it official?

dbannon

  • Hero Member
  • *****
  • Posts: 2796
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Static Array Initialise
« Reply #10 on: December 14, 2022, 02:11:04 am »
but perhaps has not made it yet into the official documentation anywhere.
Dynamic array constant expressions.
And isn't it official?

Yes, it is. My problem was I though I was making a static array (the compiler gets everything it needs to define the structure at run time!). Now I see that a bit more clearly, yes, it is documented for a dynamic array. But not a static one I suggest ?

    SArray : array[0..2] of integer = (1, 2, 3);
    DArray : array of integer  = (4, 5, 8);

That structure is just as useful in the Static Array.

Further, that page does not mention things like Insert(), Delete(), Concat() also announced in PascalDragon's mailing list post of 2018.

I'll add something to https://wiki.freepascal.org/Array (assuming no one objects).

Davo

Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: Static Array Initialise
« Reply #11 on: December 16, 2022, 04:22:57 pm »
Yes, it is. My problem was I though I was making a static array (the compiler gets everything it needs to define the structure at run time!). Now I see that a bit more clearly, yes, it is documented for a dynamic array. But not a static one I suggest ?

Please report a bug against the documentation.

Further, that page does not mention things like Insert(), Delete(), Concat() also announced in PascalDragon's mailing list post of 2018.

Please file a bug for that as well.

 

TinyPortal © 2005-2018