Recent

Author Topic: Const record array  (Read 2745 times)

fasdfasdfasdfasdfasdf

  • New Member
  • *
  • Posts: 45
Const record array
« on: January 14, 2019, 06:50:19 pm »
Why doesn't this compile with {$mode delphi} but does without it?

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3.  
  4. {$mode delphi}
  5.  
  6. type
  7.     TValues = record
  8.       Items: array of record
  9.         Name: PAnsiChar;
  10.       end;
  11.       Ages: array of integer;
  12.       Name: PAnsiChar;
  13.     end;
  14.  
  15.  const
  16.     Items : array of TValues =
  17.     (
  18.       ( Items:( (name: 'asda'), (name: 'asdsa')   );  Ages:(1); Name : 'Clock';),
  19.       ( Items:( (name: 'asda'), (name: 'asdsa')   );  Ages:(1); Name : 'Clock';),
  20.       ( Items:( (name: 'asda'), (name: 'asdsa')   );  Ages:(1); Name : 'Clock';)
  21.     ) ;
  22.  
  23. begin
  24.  
  25.  
  26. end.  

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Const record array
« Reply #1 on: January 14, 2019, 07:07:40 pm »
Delphi probably doesn't support it so FPC in {$mode delphi} doesn't too.
BTW, if you know number of elements at design-time then you don't need dynamic array.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

fasdfasdfasdfasdfasdf

  • New Member
  • *
  • Posts: 45
Re: Const record array
« Reply #2 on: January 14, 2019, 08:08:12 pm »
Pretty sure there is a workaround for this..

440bx

  • Hero Member
  • *****
  • Posts: 4015
Re: Const record array
« Reply #3 on: January 14, 2019, 08:46:44 pm »
Pretty sure there is a workaround for this..
Honestly, the first thing I would "fix" in the code you provided is the field and variable naming.   Even in an example, it's painful to read.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

furious programming

  • Hero Member
  • *****
  • Posts: 858
Re: Const record array
« Reply #4 on: January 14, 2019, 11:31:35 pm »
@440bx: the types and variables naming is good (in accordance with the adopted naming convention). At most, OP should correct the name of the constant (should be in SNAKE_UPPER_CASE) and unify the indents.

The problem is that the online syntax highlighter (in this forum too) treats the name word as reserved word, not as keyword, so it sets the bold style for these words. However, the code editor in Lazarus easily distinguishes the context and always sets the correct style.
« Last Edit: January 14, 2019, 11:34:22 pm by furious programming »
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.

jamie

  • Hero Member
  • *****
  • Posts: 6129
Re: Const record array
« Reply #5 on: January 14, 2019, 11:36:42 pm »
I believe you can place a "&" or was it a "%" infront of reserved words to be used as variables ..

&End;
for example
The only true wisdom is knowing you know nothing

furious programming

  • Hero Member
  • *****
  • Posts: 858
Re: Const record array
« Reply #6 on: January 14, 2019, 11:40:15 pm »
@jamie: the proper prefix is the & character. Only that the & prefix does not serve to mindlessly use reserved words as identifiers. It is not recommended.

The percent character is used as the prefix for binary number (literal), for example %1100110.
« Last Edit: January 14, 2019, 11:42:47 pm by furious programming »
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.

jamie

  • Hero Member
  • *****
  • Posts: 6129
Re: Const record array
« Reply #7 on: January 15, 2019, 12:02:55 am »
Roger That! ;D
The only true wisdom is knowing you know nothing

PascalDragon

  • Hero Member
  • *****
  • Posts: 5469
  • Compiler Developer
Re: Const record array
« Reply #8 on: January 15, 2019, 09:03:47 am »
Why doesn't this compile with {$mode delphi} but does without it?

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3.  
  4. {$mode delphi}
  5.  
  6. type
  7.     TValues = record
  8.       Items: array of record
  9.         Name: PAnsiChar;
  10.       end;
  11.       Ages: array of integer;
  12.       Name: PAnsiChar;
  13.     end;
  14.  
  15.  const
  16.     Items : array of TValues =
  17.     (
  18.       ( Items:( (name: 'asda'), (name: 'asdsa')   );  Ages:(1); Name : 'Clock';),
  19.       ( Items:( (name: 'asda'), (name: 'asdsa')   );  Ages:(1); Name : 'Clock';),
  20.       ( Items:( (name: 'asda'), (name: 'asdsa')   );  Ages:(1); Name : 'Clock';)
  21.     ) ;
  22.  
  23. begin
  24.  
  25.  
  26. end.  

Delphi requires [...] for dynamic array constants, so FPC requires them in mode Delphi as well instead of the more logical (...):-X

Code: Pascal  [Select][+][-]
  1.  const
  2.     Items : array of TValues =
  3.     [
  4.       ( Items:[ (name: 'asda'), (name: 'asdsa')   ];  Ages:[1]; Name : 'Clock';),
  5.       ( Items:[ (name: 'asda'), (name: 'asdsa')   ];  Ages:[1]; Name : 'Clock';),
  6.       ( Items:[ (name: 'asda'), (name: 'asdsa')   ];  Ages:[1]; Name : 'Clock';)
  7.     ] ;
  8.  

fasdfasdfasdfasdfasdf

  • New Member
  • *
  • Posts: 45
Re: Const record array
« Reply #9 on: January 15, 2019, 01:35:51 pm »
Oh..  :o

 

TinyPortal © 2005-2018