[fpc-pascal] generic proc inference

Mattias Gaertner nc-gaertnma at netcologne.de
Mon Oct 7 23:03:12 CEST 2019


On Mon, 7 Oct 2019 16:35:42 -0400
Ryan Joseph <genericptr at gmail.com> wrote:

>[...]
> procedure Run(a: integer; b: string);
> begin
> end;
> 
> generic procedure Run<S,T>(a: S; b: T; c: integer);
> begin
> end;
> 
> 
> // fails because the non-generic Run() will always be picked and the
> parameter count will be wrong. Run(1,1,1);

Yes, that works in Delphi.

If you want to be better than Delphi, support this:

{$mode objfpc}
type
  TProc<T> = procedure(a: T);
  TAnt = class
    generic procedure Run<T: class>(a: TProc<T>);
  end;
  TBird = class end;
generic procedure TAnt.Run<T>(a: TProc<T>);
begin
end;
procedure Fly(Bird: TBird);
begin 
end;
var obj: TAnt;
begin
  obj.Run(@Fly);
  // when fpc supports anonymous functions:
  obj.Run(procedure Fly(Bird: TBird) begin end);
end.


Mattias


More information about the fpc-pascal mailing list