8

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...

4 Answers 4

14

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

5

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

4

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.

3

As others have said it's almost certainly the {$J+} directive.

If you have a diff tool you can compare the project files to see how they differ - they're just text files - to solve similar problems in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.