"Identifier Expected" 或 "Invalid Prototype" 在 Inno Setup 中实现脚本常量时

"Identifier Expected" or "Invalid Prototype" when implementing a scripted constant in Inno Setup

因此,鉴于此函数,我在 GetRoot := ROOTPage.Values[0]; 行中收到错误“预期标识符”。我希望它告诉我 ROOTPage 未定义?

const
  DefaultRoot = 'C:\IAmGRoot';
Var
  ROOTPage : TInputQueryWizardPage;

procedure SetupRoot;
begin
  ROOTPage := CreateInputQueryPage(wpUserInfo,
    ExpandConstant('{cm:RootTitle}'), 
    ExpandConstant('{cm:RootInstructions}'),
    ExpandConstant('{cm:RootDescription}') + ' "' + DefaultRoot + '"'
    );
  
  ROOTPage.Add(ExpandConstant('{cm:SSRoot}') + ':', False);
  ROOTPage.Values[0] := ExpandConstant('{DefaultRoot}');

  // add SSROOT to path
end;

function GetRoot : string;
begin
  GetRoot := ROOTPage.Values[0];
end;

我应该如何解释这个错误。 Pascal 中的标识符是什么?

这个page告诉我标识符是变量名。也许我需要以某种方式扩展 ROOTPage.Values[0],因为我从 Inno Setup 对象引用数组?

或者我可能需要 return 不同的值。我在 Pascal 上看到 one page 说你需要避免在参数较少的函数上分配函数值以避免递归循环。这是否意味着我应该传递一个虚拟值?还是有不同的语法?那个页面没有解释。

我私下认为我真正的问题是我没有正确定义我的功能......但是好吧。至少编译这么多。 这个问题可以变成:你如何处理 Pascal 中的无参数函数?

我认为 Inno Setup 不是问题的一部分,但我正在使用 Inno Setup 以防万一。


更新: 它似乎不是数组,因为它得到了同样的错误:

const
  DefaultRoot = 'C:\IAmGRoot';

function GetRoot : string;
begin
  GetRoot := DefaultRoot;
end;

更新: 这个link已经说过函数名可以替换/应该用关键字Result替换比如下面的代码。我实际上知道这一点,但 Inno Setup 编译器不将其识别为有效语法。然后它告诉我我的函数是一个无效的原型。

function GetRoot : string;
begin
  Result := DefaultRoot;
end;

更新: 如果我这样做,我会得到“GetRoot 的无效原型”

function GetRoot : boolean;
begin
  Result := False;
end;

@Martin Prikryl 更新:

好吧,我在几个地方使用它,但典型的用法是这样的:

[Files]
Source: "C:\ValidPath\Release\*"; DestDir: "{app}\bin"; Components: DefinedComponent
Source: "C:\ValidPath\Deployment\*"; DestDir: "{code:GetRoot}\"; Flags: ignoreversion recursesubdirs; Components: DefinedComponent

需要标识符

您的代码在 Pascal 中是正确的,但在 Pascal 脚本中无法编译。

在 Pascal 中,当您想要为函数分配 return 值时,您可以将该值分配给具有函数名称的 "variable" 或分配给 Result 变量.

所以这是正确的:

function GetRoot: string;
begin
  GetRoot := ROOTPage.Values[0];
end;

还有这个(两者等价):

function GetRoot: string;
begin
  Result := ROOTPage.Values[0];
end;

在 Pascal 脚本中,只有 Result 有效。当您使用函数名称时,您会得到 "Identifier expected."


原型无效

当从 Code 部分外部调用函数并且需要特定参数 list/return 值时,您会得到这个。但是你没有告诉我们,你使用 GetRoot 函数的目的是什么。

Inno Setup 中有两个地方可以使用自定义函数:

  • Check parameter:为此,函数必须 return a Boolean 并且不带参数或带一个参数(参数类型由您指定的值决定)在 Check 参数中提供)。

    function MyProgCheck(): Boolean;
    
    function MyDirCheck(DirName: String): Boolean;
    
  • Scripted Constants:该函数必须 return 一个 string 并带有一个 string 参数,即使脚本常量中没有提供任何参数.我假设这是您的用例。如果您不需要任何参数,只需声明它,但不要使用它:

    function GetRoot(Param: String): string;
    begin
      Result := ROOTPage.Values[0];
    end;