[fpc-pascal] Feature announcement: implicit generic function specializations
nc-gaertnma at netcologne.de
Fri Apr 22 21:30:21 CEST 2022
On Fri, 22 Apr 2022 20:51:56 +0200
Martin Frb via fpc-pascal <fpc-pascal at lists.freepascal.org> wrote:
> I did explore what happens if I throw different types at it, and see
> how the current implementation deals with this (what I call) lack of
> type safety.
> And also asked the question, how should it act? (Because the current
> behaviour is new, expected to need fixes, and can obviously be fixed).
See Sven's first mail.
> So what happens if:
> b: Byte;
> c: Cardinal;
> Add(b, c);
> 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).
Yes, as explained by Sven.
> If you use numeric constants:
> writeln(' 0', Add(0, 0) ); // ShortInt
> writeln('1000', Add(1000, 1000) ); // SmallInt
> writeln('100K', Add(100000, 100000) ); // Integer
> So then, if you try to fix " Add(b, c)" by checking that b and c
> have the same type => "Add(c, 0)" will fail => because 0 is ShortInt
> and not cardinal.
Why is that a fail?
> I created a little test program (see bottom of mail).
> And it gives a strange warning:
> Compile Project, Target:
> C:\Users\martin\AppData\Local\Temp\project1.exe: Success, Warnings:
> 1, Hints: 3
> project1.lpr(67,52) Warning: Range check error while evaluating
> constants (100000 must be between -128 and 127)
Correct range error.
> project1.lpr(41,18) Hint: Local proc "Add$1" is not used
> 72 lines compiled, 0.2 sec, 105136 bytes code, 5476 bytes data
> Line 41 is the declaration of the generic
> generic function Add<T>(aArg1, aArg2: T): T;
Maybe related to
More information about the fpc-pascal