result := v + result
Delphi's Variant
type is a slightly extended version of the Win32 API's VARIANT
type 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
:
Condition Result
Both expressions are strings Concatenated
[...]
One expression is numeric and the other a string Addition
[...]
I suspect VarAdd
is defined like that to make things easier for VB users.
result := result + '-' + v
Here result + '-'
should perform string concatenation since both operands are strings. '3-' + v
is then treated as a numeric addition, requiring 3-
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
. Adding 2
to that results in -1
.