【问题标题】:Generic nested class function in Free PascalFree Pascal 中的通用嵌套类函数
【发布时间】:2018-03-27 23:17:42
【问题描述】:

我尝试编写以下代码,看看是否可以通过结合对泛型类函数和嵌套类的支持来模拟 Free Pascal 3 中的泛型方法:

{$mode delphi}
type TFoo = class
  public
    type TBar<T> = class
      class function Min(const A, B: T): T;
    end;
end;
class function TFoo.TBar<T>.Min(const A, B: T): T;
begin
  if A < B then
    Result := A
  else
    Result := B;
end;

我尝试了几种语法变体,但无论如何我都无法编译它。在这种形式下,编译器在第 8 行给了我一个致命错误(method identifier expectedSyntax error, ";" expected but "&lt;" found)。

如果可能的话,正确的语法是什么?

【问题讨论】:

  • 在 Delphi 中运行良好,所以它一定是 FreePascal 错误。询问FreePascal forums,如果需要file a bug report
  • @Remy,A &lt; B 如何为T 工作任何地方?如果你发现在 Delphi 中可以编译,你应该向 EMBT 提交错误报告;-)
  • @Victoria: A &lt; B 在没有类型转换的情况下不起作用(并且使用类型转换,代码在 Delphi 中运行良好),但这不是问题所在。错误出现在 class function TFoo.TBar&lt;T&gt;.Min(const A, B: T): T; 上,并且在 Delphi 中编译。
  • @Remy,这就是我所依赖的。只是不值得在 Delphi 中说 这很好(对于未来的访问者);-) 但是,我同意,我可以使用 FPC 3.0.2 编译器重现同样的问题。
  • 可编译并在 FPC 中继 BTW 中工作。

标签: freepascal


【解决方案1】:

在 fpc 3.0.4 中正确且有效的语法,至少在 objfpc 模式下(模式 delphi 未测试):

{$mode objfpc}
type TFoo = class
  public
    type generic TBar<T> = class
      class function Min(const A, B: T): T;
    end;
end;
class function TFoo.TBar.Min(const A, B: T): T;
begin
  ...
end;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多