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

标签 function syntax inno-setup pascalscript

所以给定这个函数,我在 GetRoot := ROOTPage.Values[0]; 上得到错误“Identifier Expected”。线。我希望它告诉我 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 对象引用了一个数组?
或者也许我需要以不同的方式返回值。我看到了one page在 Pascal 上说您需要避免在无参数函数上分配函数值以避免递归循环。这是否意味着我应该传入一个虚拟值?还是有不同的语法?那一页没有解释。
我暗中认为我真正的问题是我没有正确定义我的功能......但是很好。至少可以编译这么多。 这个问题可能变成:如何在 Pascal 中处理无参数函数?
我不认为 Inno Setup 是问题的一部分,但我正在使用 Inno Setup 以防万一。

更新:
它似乎不是数组,因为这会得到相同的错误:
const
  DefaultRoot = 'C:\IAmGRoot';

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

更新:
这个link已经说过函数名可以替换/应该替换成关键字Result比如下面的代码。我实际上知道这一点,但 Inno Setup 编译器不认为这是有效的语法。然后它告诉我我的函数是一个无效的原型(prototype)。
function GetRoot : string;
begin
  Result := DefaultRoot;
end;

更新:
如果我这样做,我会得到“GetRoot 的无效原型(prototype)”
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 中,当您要分配函数的返回值时,您可以将该值分配给带有函数名称的“变量”,或者分配给 Result。多变的。

所以这是正确的:

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

这也是(两者都是等价的):

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

在 Pascal 脚本中,只有 Result作品。当您使用函数的名称时,您会得到“预期的标识符”。

无效的原型(prototype)

当函数从 Code 外部调用时,你会得到这个。部分和一个特定的参数列表/返回值是必需的。但是你没有告诉我们,你用的是什么GetRoot为。

有两个地方可以在 Inno Setup 中使用自定义函数:
  • Check parameter : 为此,函数必须返回 Boolean并且不带参数或只带一个参数(参数类型由您在 Check 参数中提供的值确定)。

    function MyProgCheck(): Boolean;
    
    function MyDirCheck(DirName: String): Boolean;
    
  • Scripted Constants : 函数必须返回 string拿一张string参数,即使脚本常量中没有提供参数。我认为这是您的用例。如果您不需要任何参数,只需声明它,但不要使用它:

    function GetRoot(Param: String): string;
    begin
      Result := ROOTPage.Values[0];
    end;
    
  • 关于function - 在 Inno Setup 中实现脚本常量时为 "Identifier Expected"或 "Invalid Prototype",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33656548/

    相关文章:

    java - 为什么 Java 8 中没有 BooleanConsumer?

    bash - 使用 unset 与将变量设置为空

    inno-setup - Inno Setup 卸载程序 VersionInfo

    inno-setup - Inno Setup - #define 指令 - 如何使用先前定义的变量?

    ios - 无法将不可变值作为输入参数 : literals are not mutable, 传递,为什么?

    function - Powershell Cmdlet.ShouldProcess 方法默认答案

    function - 如何调用名称为变量的函数?

    c# - 如何同时为一个 C# 类分配多个名称

    syntax - 匹配借用的枚举——为什么这个语法是等价的?

    installation - Innosetup - 在安装开始之前引用[Code]中的文件