2
votes

I have the following sample code in my DUnitX test project that fails to compile. This sample code is scoped identically to code that compiles without errors in a very large VCL forms project. Both projects are using Delphi XE4, yet when I reference the source file that compiles successfully in the VCL project in my DUnitX unit test project, it fails with the same E2064 left side cannot be assigned to this code produces:

program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

type
  TTestRec = record
    A: string;
  end;

const
  AConstArray : array [0..1] of TTestRec = ( (A: '1'), (A: '2') );

procedure E2064Test;
begin
  {$J+}
  {$WRITEABLECONST ON}
  AConstArray[0].A := '3'; // error here
  AConstArray[1].A := '4'; // error here
  {$WRITEABLECONST OFF}
  {$J-}
end;

begin
end.

Is there a compiler switch or some other strange path/setting that I need to specify to get this code to compile for my DUnitX test project in XE4?

2
Just curious, since you are writing data into the array, why not declare the array as var instead of const?Remy Lebeau
Er, $WRITEABLECONST and $J are the same thingDavid Heffernan
Yes, David. Thanks for making sure :)John Kaster

2 Answers

2
votes

You can use {$J+}. This compiles for me.

type
  TTestRec = record
    A: string;
  end;

const
  {$J+}
  AConstArray : array [0..1] of TTestRec = ( (A: '1'), (A: '2') );
  {$J-}

procedure E2064Test;
begin
  AConstArray[0].A := '3'; // error here
  AConstArray[1].A := '4'; // error here
end;
0
votes

With the help of Graymatter, I found the place to set the switch.

Click the checkbox under:

Project options | Delphi compiler | Compiling | Syntax | Assignable types constants

Update: As graymatter points out, the compiler switches, if used, need to be where the symbol is declared, not accessed.