[fpc-pascal] generic proc inference

Mattias Gaertner nc-gaertnma at netcologne.de
Mon Oct 7 11:59:35 CEST 2019


On Mon, 7 Oct 2019 07:54:51 +0200
Sven Barth via fpc-pascal <fpc-pascal at lists.freepascal.org> wrote:

> Ryan Joseph <genericptr at gmail.com> schrieb am Mo., 7. Okt. 2019,
> 00:16:
>[...] 
> > >>> 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;  
> >
> > This is debatable I think but I understand why "Ambiguous call to
> > DoThis” would make sense. I’m pretty sure C# does this also.
> >
> > What is the rule then? I’ll have to think about that some more.
> >  
> 
> There is nothing debatable here. If a generic parameter is not used
> in the parameter list (cause it's used in the result type (either
> directly or to specialize something else), only the body of the
> routine or even not at all), then that routine must not be used for
> implicit specialization.

Yes, but does that mean to tell the programmer, that he is using these
overloads the wrong way (as Delphi does), or should the compiler silently skip invalid overloads.
For the sake of don't-point-the-gun-at-my-foot, I would vote for the
first.

Mattias


More information about the fpc-pascal mailing list