[fpc-pascal] generic proc inference

Mattias Gaertner nc-gaertnma at netcologne.de
Sun Oct 6 23:40:00 CEST 2019


On Sun, 6 Oct 2019 17:03:10 -0400
Ryan Joseph <genericptr at gmail.com> wrote:

>[...]
> > 2.
> > DoThis(1,200) gives range check warning instead of error. A warning
> > means there are some rare cases, where this code is correct. Is
> > this a todo or do you see a useful case?  
> 
> Which test? Please post a sample.

generic procedure DoThis<T>(a:T; b:T);
begin end;

begin
  DoThis(1,200); // 1 sets T to shortint, so the 200 gives a warning
end;

 
> > 3.
> > timpfuncspez2.pp
> > DoThis<T>
> > DoThis<T,U>
> > Delphi gives an error "Ambiguous call to DoThis". FPC silently
> > selects the one with only one param. IMO this is dangerous, it
> > should give an error.  

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

begin
  DoThis(3); // both fits, should give an error
end;

 
>[...]
> > 4.
> > Why does timpfuncspez6 fail?
> > It works in Delphi.
> > The comment has an explanation, which looks wrong to me:
> > 
> > generic procedure DoThis<T,U>(msg: T; param1: U; param2: TObject);
> > begin
> > end;
> > 
> > begin
> >        DoThis('aa', 'aa', TObject.Create);
> >        // wil be specialized as DoThis(msg: integer; param1:
> > TObject; param2: TObject)
> >        // so we expect an incompatible type error
> >        DoThis(1, 1, TObject.Create);
> > end.  
> 
> That doesn’t make sense to me either so I need to study it. Both
> should fail actually as I designed it (for now).

Why?

 
> How does Delphi implicitly specialize this?

DoThis(1,1,nil); // T and U become shortint
DoThis('aa','aa',nil); // T and U become string

Mattias


More information about the fpc-pascal mailing list