【发布时间】:2015-07-25 16:56:28
【问题描述】:
编译匿名函数的本地函数时(显然)出现虚假警告的原因是什么,以及如何消除它们。
一个简单的函数编译干净 - 没有警告。如果该函数是另一个函数的局部函数,则再次没有警告。如果该函数是匿名函数的本地函数,则会产生以下警告:
[DCC Warning] Unit1.pas(57): W1036 Variable 'i' might not have been initialized
[DCC Warning] Unit1.pas(58): W1035 Return value of function 'StrToJType' might be undefined
示例代码如下所示。请注意,尽管此代码可以编译,但它会给出与此问题无关的警告,因为它不完整。
编辑 评论和回复表明,示例代码在匿名函数中不包含返回值这一事实可能是问题的原因。此编辑修改了代码以解决此问题,以简化本地函数案例并最小化代码。问题还是一样。
unit Unit1;
interface
type
JType = (JAtLeastOnce, JConditionLine, JInfix, JIteration, JNonNullInfix );
TFuncTest = reference to function : JType;
function StrToJType(aString : string) : JType;
implementation
function StrToJType(aString : string) : JType;
// Basic function - does not give warnings
var
i : integer;
begin
i := Pos(aString, '+i*-?');
if i <> 0 then result := JType(i - 1) else result := High(JType);
end;
function Test : JType;
// Local function - does not give warnings
function StrToJType(aString : string) : JType;
var
i : integer;
begin
i := Pos(aString, '+i*-?');
if i <> 0 then result := JType(i - 1) else result := High(JType);
end;
begin
result := low(JType);
end;
function Test2 : TFuncTest;
// Local function of anonymous function - gives warnings
begin
result :=
function : JType
function StrToJType(aString : string) : JType;
var
i : integer;
begin
i := Pos(aString, '+i*-?');
if i <> 0 then result := JType(i - 1) else result := High(JType);
end;
begin
result := Low(JType);
end;
end;
end.
【问题讨论】:
-
是在抱怨 StrToJType 还是匿名函数?由于匿名函数不设置结果,是否会混淆这两者?
-
关于您的编辑,您感到惊讶吗?你做了最小的复制了吗?完成后,按照我在回答中的建议将其提交给 QP。
-
只是一个疯狂的猜测,但我相信其中一些功能是在 D2010 中添加的,所以到 XE 出来时可能仍然存在奇怪的错误。另外,为什么要在其中制作一个包含无关代码的示例?即,从未引用过的第一个匿名方法 Test 中的本地函数。只是好奇。
-
Test 中的本地函数是我对最少代码的尝试——无论是否引用了基本函数,都没有警告;用作本地函数时无,无论是否引用);但在用作匿名函数的本地函数时确实会发出警告。
标签: function delphi compiler-warnings delphi-xe