Nested constants of nested structured types not supported?

Despite what Delphi says,

structured types may contain other structured types; a type can have an unlimited level of structuring

with the notable exception that structured typed constants

cannot contain file type values ​​at any level

I found that I can not use the write constant as an element of an array constant of the same type.

Testcase

type
  MyRecord = record MyField: Integer end;

const
  Typical: array[0..1] of MyRecord = ((MyField: 0), (MyField: 1));

  { now I tried to achieve more clarity by declaring a specific constant }
  Zero: MyRecord = (MyField: 0);

  { and compiler refused to accept that }
  Bad: array[0..1] of MyRecord = (Zero, (MyField: 1));  { E2029 '(' expected but identifier 'Zero' found }

I tested this code with several Borland compilers, they all showed the same behavior. UPD: the same for FPC, but not for GPC (!).

Question (s)

? " " ? ?

+5
2

, , , ZeroRec - , .

{$ WRITEABLECONSTST ON}, . $WRITEABLECONST OFF - ( XE2):

program Project3;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils;

type
  MyRecord = record MyField: Integer end;
  PMyRecord = ^MyRecord;

const
  Typical: array[0..1] of MyRecord = ((MyField: 0), (MyField: 1));

  { now I tried to achieve more clarity by declaring a specific constant }
  ZeroRec: MyRecord = (MyField: 0);
  { and compiler refused to accept that }
//  Bad: array[0..1] of MyRecord = ((MyField: Zero), (MyField: 1));  { E2029 '(' expected but identifier 'Zero' found }
begin
  try
    { TODO -oUser -cConsole Main : Insert code here }
    WriteLn(ZeroRec.MyField);
    PMyRecord(@ZeroRec)^.MyField := 2;
    WriteLn(ZeroRec.MyField);
    readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

0

2

,

  Zero = 0;
  ZeroRec: MyRecord = (MyField: Zero);

, ,

  Zero : Integer = 0;
  ZeroRec: MyRecord = (MyField: Zero);

[DCC Error] Project3.dpr(19): E2026

+5

, , Typed Constant. Array Constant. ( ):

, , , . .

, , - , , .

Delphi, .

+3

All Articles