[fpc-devel] Implicit function specialization precedence

Sven Barth pascaldragon at googlemail.com
Thu Apr 8 23:48:49 CEST 2021


Am 07.04.2021 um 23:21 schrieb Ryan Joseph via fpc-devel:
> With the requested changes I believe some precedence rules have changed. These both should be "Can't determine which overloaded function to call" errors or the non-generic should take precedence because the functions are ambiguous (by appearance at least). Currently the compiler thinks DoThis<T> is better than the non-generic and this may be because it was specialized as DoThis<ShortInt> because the parameter of "1" is a ShortInt.
>
> What should the rule be here?
>
> =====================
>
> procedure DoThis(a: word; b: word);
> begin
> end;
>
> generic procedure DoThis<T>(a:T; b: word);
> begin
> end;
>
> begin
>    DoThis(1,1); // DoThis<T>
> end.

In Delphi this takes the non-generic. But I think this comes into a 
region where things are not clearly documented, thus it's hard to get 
things "right". In theory one could move the generic check in front of 
the ordinal check and it should work, but it could get wonky again if 
you pass e.g. a LongInt into this (Delphi keeps insisting on using the 
non-generic with a LongInt constant parameter even though if you have 
two non-generic overloads it will complain that it violates the range of 
Word and ShortInt... go figure... - with a LongInt variable it will use 
the generic)

Let it be as it is now... this will only result in headaches either way.

> =====================
>
> generic procedure DoThis<T>(a:T; b: word);
> begin
> end;
>
> generic procedure DoThis<T>(a: word; b: T);
> begin
> end;
>
> begin
>    DoThis(1,1); // Can't determine which overloaded function to call
> end.
>
> =====================

This is correct. Delphi behaves the same here (and it would be the same 
in the non-generic case with T being replaced by ShortInt).

Regards,
Sven


More information about the fpc-devel mailing list