[fpc-devel] Abs(Variant) unexpected result

Michael Van Canneyt michael at freepascal.org
Sun Feb 25 17:34:16 CET 2018



On Sun, 25 Feb 2018, Bart wrote:

> On Sun, Feb 25, 2018 at 5:01 PM, Jonas Maebe <jonas at freepascal.org> wrote:
>
>> As Michael said, overloads are selected at compile time. This is true for
>> both FPC and Delphi. We even have over a 100 unit tests that we ran under
>> Delphi to reverse engineer their selection priorities in case of variants:
>> https://svn.freepascal.org/svn/fpc/trunk/tests/test/cg/variants/
>>
>> Abs(), however, gets a forced conversion to float in Delphi:
>> https://bugs.freepascal.org/view.php?id=20551
>
> Hmm, did not find that one (I searched bugtracker before posting here).
>
>
> Seems you are right:
>
> Delphi Tokyo 10.2
>
> X = -1
> VarIsFloat : FALSE
> VarIsNumeric: TRUE
> VarIsOrdinal: TRUE
> After Abs()
> X = 1
> VarIsFloat : TRUE
> VarIsNumeric: TRUE
> VarIsOrdinal: FALSE
>
> So, would it be possible to have an overloaded Abs(V: Variant):
> Variant; function in the variants unit?

I advise against it.

S : String;

begin
   S:='My very nice string';
   S:=Abs(S);
end;

Will then compile and give a run-time error, as opposed to a compile-time
error now.

Michael.



More information about the fpc-devel mailing list