The way adding strings and variants behaves in Delphi (10.2 Tokyo) was a complete surprise to me. Can someone provide a reasonable explanation for this "feature" or shall we call it a bug ?
function unexpected: string;
var v: Variant;
begin
result := '3';
v := 2;
result := v + result;
ShowMessage(result); //displays 5, I expected 23
result := '3';
v := 2;
result := result + '-' + v;
ShowMessage(result) //displays -1, I expected 3-2
end;
result := v + resultDelphi's
Varianttype is a slightly extended version of the Win32 API'sVARIANTtype and supposed to be compatible with it so long as you do not use any Delphi-specific types. Additionally, when you use Delphi-specific string types, it is supposed to behave like it would with the OLE string type. In the Win32 API, it is specifically documented that adding a string and a number will result in a (numeric) addition, not a string concatenation, that you need to have two string operands to get a string concatenation:VarAdd:I suspect
VarAddis defined like that to make things easier for VB users.result := result + '-' + vHere
result + '-'should perform string concatenation since both operands are strings.'3-' + vis then treated as a numeric addition, requiring3-to be parsed as a number. I believe that since there are contexts in which the sign follows the digits, this parse succeeds and produces-3. Adding2to that results in-1.