2

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?

John Kaster
  • 2,509
  • 1
  • 33
  • 40

2 Answers2

2

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;
Graymatter
  • 6,529
  • 2
  • 30
  • 50
  • Thanks for the link and the very quick reply. I couldn't figure out the right terms to googlebing. Unfortunately, in my DUnitX project, it's still not compiling. I've tried both {$J+} and {$WRITEABLECONST ON}. I'll create a single console app and see if it behaves differently there. Again, I'm in XE4 – John Kaster Jun 20 '14 at 00:11
  • I cannot get this to compile in XE4. I'm updating my question to have completely stand-alone code as a console project. – John Kaster Jun 20 '14 at 00:15
  • @JohnKaster I am using XE4. I had the directive in the wrong place. I first set the project options which made it work. The code above should now work. – Graymatter Jun 20 '14 at 00:20
  • Heh. I see you found the same answer I did (we must have been typing simultaneously). Crediting you with the win. Thanks! – John Kaster Jun 20 '14 at 00:25
0

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.

John Kaster
  • 2,509
  • 1
  • 33
  • 40
  • 2
    They are not ignored. They need to be around the constant, not the code accessing the constant as it relates to how the object is defined. – Graymatter Jun 20 '14 at 00:27
  • Gotcha. However, when the switch is on, they don't need to be there at all. – John Kaster Jun 20 '14 at 00:42
  • 1
    I also don't understand why you want to use writeable typed constants. These are an anachronism from the 20th century. Your are not expected to use this switch any more. – David Heffernan Jun 20 '14 at 11:15