0
votes

I tried writing the following to see if I could simulate generic methods in Free Pascal 3 by combining its support for generic class functions and nested classes:

{$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;

I have tried several syntactical variations, but I can't get it to compile no matter what. In this form, the compiler gives me a fatal error on line 8 (method identifier expected, Syntax error, ";" expected but "<" found).

What is the proper syntax for this, if at all possible?

1
Works fine in Delphi, so it must be a FreePascal bug. Ask in the FreePascal forums, and if needed file a bug report. - Remy Lebeau
@Remy, how A < B can work for T anywhere? If you find this compilable in Delphi, you should file a bug report to EMBT ;-) - Victoria
@Victoria: A < B doesn't work without type-casting (and with a type-cast, the code works fine in Delphi), but that is not the issue in question. The error is on class function TFoo.TBar<T>.Min(const A, B: T): T; and that compiles in Delphi. - Remy Lebeau
@Remy, that's what I reliazed. Just not worth saying this works fine in Delphi (for future visitors) ;-) But yes, I agree, I could reproduce the same issue with FPC 3.0.2 compiler. - Victoria
Compilable and works in FPC trunk BTW. - Abelisto

1 Answers

0
votes

Correct and working syntax in fpc 3.0.4, at least in mode objfpc (mode delphi not tested):

{$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;