*有时*在 Delphi 中分配给常量时会出现错误
我正在使用带有所有补丁和更新的 Delphi 2007。
我有一个由两个不同项目使用的文件。 该文件中有一个过程,我将其简化如下:
procedure MyProcedure;
const
UniqueValue: integer = 0;
begin
//some code
Inc(UniqueValue);
//some more code
end;
Inc() 命令应该失败,因为您无法分配给常量。 在一个项目中,我收到了一个与此相关的错误(我将该项目称为“准确”)。 在另一个项目中,我没有收到任何错误(我将其称为“Bogus”)。 我也没有收到任何警告。 我不明白为什么编译器会让这个不正确的代码通过。
这是我在 Bogus 项目中尝试过的方法:
1 - 引入一个明显的错误,例如在一行中间键入“slkdjflskdjf”
结果:我收到一个错误,这证明它确实在尝试编译此文件。
2 - 删除.DCU,并重建项目
结果:.DCU 重新生成,再次证明该项目确实编译了此错误代码。
有谁想过为什么会发生这种行为? 更具体地说,为什么它会发生在一个项目中而不是另一个项目中? 是否有一些晦涩的编译器选项允许分配给常量?
最后一点:这两个项目都是从 Delphi 5 转换而来。在 Delphi 5 下,使用类似的代码,它们都编译得很好。
编辑:感谢您的帮助。 更改可分配类型常量指令后,我可以在两个项目中获得一致的行为。 我今天学到了一些新东西......
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(4)
正如其他人所说,这几乎肯定是 {$J+}
指令。
如果您有 diff 工具,您可以比较项目文件以了解它们有何不同 - 它们只是文本文件 - 以解决将来的类似问题。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
这些项目是否在 $J 编译器指令的设置上有所不同?
Could it be that those projects differ in the setting of the $J compiler directive?