[fpc-pascal] Feature announcement: implicit generic function specializations

Mattias Gaertner nc-gaertnma at netcologne.de
Fri Apr 22 22:22:27 CEST 2022


On Fri, 22 Apr 2022 20:51:56 +0200
Martin Frb via fpc-pascal <fpc-pascal at lists.freepascal.org> wrote:

>[...]
> Well, I tested: It uses the type of the first Param. So it calls a 
> function for both param of type Byte. The cardinal argument is 
> converted. (potentially truncated).

I agree that Delphi should have used a better algorithm, like checking
the var/out arguments first and otherwise use the smallest
integer that fits all related params. Same for strings and boolean.
pas2js uses a similar algorithm. 

In $mode Delphi fpc must use the Delphi algorithm. In other modes it
could use a better algorithm.
If fpc would use the above algorithm in mode objfpc, the Add would work
like FPC's add:

generic function Add<T>(a,b: T): T;
begin
  Result:=a+b;
end;

Add(aByte,aCardinal) -> cardinal


And you don't need to put a var/out param leftmost:

generic procedure Adding<T>(a,b: T; out c: T);
begin
  c:=a+b;
end;

Adding(3,4,aWord);


Mattias



More information about the fpc-pascal mailing list