[fpc-pascal] generic proc inference

Ryan Joseph genericptr at gmail.com
Mon Oct 7 15:51:47 CEST 2019


Right now this gives an error because you can’t add normal functions AFTER generic functions with the same name in the same scope. I can’t find the bug report but I think Sven fixed this recently or gave a better error at least. 

generic function DoThis<T>(a: T): T; overload;
begin
end;

generic function DoThis<T,U>(a: T): U; overload;
begin
end;

// overloaded identifier "DoThis" isn't a function

function DoThis(a : integer): integer; overload;
begin
end;

========================

That bug aside the rule is that non-generic overloads always win over generics. I remember this was discussed at length if we can find the old thread.

{$mode objfpc}
{$modeswitch implicitfunctionspecialization}

program timpfuncspez14;

type
  TMyClass = class
  end;

procedure DoThis(msg: TObject);
begin
  writeln('DoThis:',msg.ClassName);
end;

generic procedure DoThis<T>(msg: T);
begin
  writeln('DoThis<T>:',msg.ClassName);
end;

begin
  DoThis(TMyClass.Create);
  DoThis(TObject.Create);
end.

prints:

DoThis:TMyClass
DoThis:TObject


> On Oct 7, 2019, at 8:37 AM, Michael Van Canneyt <michael at freepascal.org> wrote:
> 
> I think sven means if you have e.g. 3 functions:
> 
> generic function DoThis<T>(a: T): T; overload;
> begin end;
> 
> generic function DoThis<T,U>(a: T): U; overload;
> begin end;
> 
> function DoThis(aInt : Integer) : Integer;
> 
> begin
> end;

Regards,
	Ryan Joseph



More information about the fpc-pascal mailing list