嵌套结构化类型的嵌套常量不受支持? [英] Nested constants of nested structured types are unsupported?
问题描述
尽管Delphi的引用说明了
结构化类型可以包含其他结构化类型;一个类型可以有无限级的结构化
具有显着的异常结构化类型常量
不能包含任何级别的文件类型值
我发现我不能使用记录常量作为同一类型数组常量的元素。
Testcase
type
MyRecord = record MyField:Integer end;
const
典型值:MyRecord =((MyField:0),(MyField:1))的数组[0..1]
{现在我试图通过声明一个特定的常量来实现更加清晰}
零:MyRecord =(MyField:0);
{和编译器拒绝接受}
MyRecord =(Zero,(MyField:1))的Bad:array [0..1]; {E2029'('expected but identifier'Zero'found}
我用几个Borland编译器测试了这个代码所有这些都表现出相同的行为UPD:对FPC而言也是一样,但不适用于GPC(!)。
问题
这里发生了什么?问题标题中的嵌套结构化类型的嵌套常量结论是否正确?对问题的更多分析?
看来这是不可能的,其根本原因是ZeroRec不是真正的常数,它更像是初始化的静态变量。 p>
如果 {$ WRITEABLECONST ON} 被指定,那么它可以轻而易举地改变,即使使用$ WRITEABLECONST OFF,也可以通过一些创意类型转换(XE2中进行测试)来更改:
程序项目3;
{$ APPTYPE CONSOLE}
{$ R * .res}
使用
System.SysUtils;
type
MyRecord = record MyField:Integer end;
PMyRecord = ^ MyRecord;
const
典型值:MyRecord =((MyField:0),(MyField:1))的数组[0..1]
{现在我试图通过声明一个特定的常量来实现更加清晰}
ZeroRec:MyRecord =(MyField:0);
{和编译器拒绝接受}
// Bad:Array [0..1] MyRecord =((MyField:Zero),(MyField:1)); {E2029'('expected but identifier'Zero'found}
begin
try
{TODO -oUser -cConsole Main:在此插入代码}
WriteLn(ZeroRec.MyField);
PMyRecord(@ZeroRec)^。MyField:= 2;
WriteLn(ZeroRec.MyField);
readln;
除了
在E:Exception do
Writeln(E.ClassName,':',E.Message);
end;
end。
这将输出
0
2
简单类型的行为也很明显,
零= 0;
ZeroRec: MyRecord =(MyField:Zero);
按预期编译,但是
零:整数= 0;
ZeroRec:MyRecord =(MyField:Zero);
给出[DCC错误] Project3.dpr(19):E2026常量表达式
Despite of what Delphi reference says
structured types can contain other structured types; a type can have unlimited levels of structuring
with notable exception what structured typed constants
cannot contain file-type values at any level
I discovered what I cannot use record constant as an element of array constant of the same type.
Testcase
type
MyRecord = record MyField: Integer end;
const
Typical: array[0..1] of MyRecord = ((MyField: 0), (MyField: 1));
{ now I tried to achieve more clarity by declaring a specific constant }
Zero: MyRecord = (MyField: 0);
{ and compiler refused to accept that }
Bad: array[0..1] of MyRecord = (Zero, (MyField: 1)); { E2029 '(' expected but identifier 'Zero' found }
I tested this code with several Borland compilers, all of them exhibited the same behaviour. UPD: also the same for FPC, but not for GPC(!).
Question(s)
What is going on here? Am I correct with "Nested constants of nested structured types are unsupported" conclusion in the question title? Any more analysis of an issue?
It appears that this isn't possible, the underlying cause is that ZeroRec isn't truly a constant, it is more like an initialised static variable.
If {$WRITEABLECONST ON} is specified, then it can be trivially changed. Even with $WRITEABLECONST OFF, it can be changed by some creative type casting (tested in XE2):
program Project3;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
type
MyRecord = record MyField: Integer end;
PMyRecord = ^MyRecord;
const
Typical: array[0..1] of MyRecord = ((MyField: 0), (MyField: 1));
{ now I tried to achieve more clarity by declaring a specific constant }
ZeroRec: MyRecord = (MyField: 0);
{ and compiler refused to accept that }
// Bad: array[0..1] of MyRecord = ((MyField: Zero), (MyField: 1)); { E2029 '(' expected but identifier 'Zero' found }
begin
try
{ TODO -oUser -cConsole Main : Insert code here }
WriteLn(ZeroRec.MyField);
PMyRecord(@ZeroRec)^.MyField := 2;
WriteLn(ZeroRec.MyField);
readln;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
This will output
0
2
The behaviour is also evident with simple types,
Zero = 0;
ZeroRec: MyRecord = (MyField: Zero);
compiles as expected, however
Zero : Integer = 0;
ZeroRec: MyRecord = (MyField: Zero);
gives [DCC Error] Project3.dpr(19): E2026 Constant expression expected
这篇关于嵌套结构化类型的嵌套常量不受支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!