Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

*Sometimes* get an error when assigning to a constant in Delphi

I am using Delphi 2007 with all patches and updates.

I have a file which is used by two different projects. In that file is a procedure, which I will simplify as follows:

procedure MyProcedure;
const
  UniqueValue: integer = 0;
begin
  //some code
  Inc(UniqueValue);
  //some more code
end;

The Inc() command should fail, because you cannot assign to a constant. In one project, I get an error to that effect (I'll call that project "Accurate"). In the other project, I don't get any errors (I'll call it "Bogus"). I also don't get any warnings. I can't figure out why the compiler lets this incorrect code through.

Here's what I've tried in project Bogus:

1 - Introduce an obvious error, like typing "slkdjflskdjf" in the middle of a line

Result: I get an error,which proves that it is really trying to compile this file.

2 - Delete the .DCU, and rebuild the project

Result: The .DCU is re-generated, again proving that the project is truly compiling this erroneous code.

Does anyone have thoughts on why this behavior would occur? And more specifically, why it would occur in one project but not another? Is there some obscure compiler option to permit assigning to constants?

One final note: Both projects are converted from Delphi 5. Under Delphi 5 with similar code, they both compile fine.

Edit: Thanks for all your help. After changing the assignable typed constants directive, I can get consistent behavior across both projects. I learned something new today...

like image 860
JosephStyons Avatar asked Apr 24 '09 15:04

JosephStyons


3 Answers

There is an option for this called "Assignable typed constants" in Compiler Options. It can also be enabled with "{$J+}" in your code.

like image 148
Ville Krumlinde Avatar answered Nov 04 '22 20:11

Ville Krumlinde


Could it be that those projects differ in the setting of the $J compiler directive?

like image 41
mghie Avatar answered Nov 04 '22 20:11

mghie


I'd suggest to check the Project Options, section Compiler and see if you can spot any differences. Maybe you have option Assignable typed constants enabled in the Bogus project.

like image 4
Diego Avatar answered Nov 04 '22 21:11

Diego