Why you should try to avoid using Variant operations in Delphi
Posted by jpluimers on 2018/04/17
[WayBack] Delphi “WAT?!” of the day: var v1, v2: Variant; begin v1 := True; v2 := True… shows some interesting code.
The [WayBack] Variant Types (Delphi) – RAD Studio: Variant Type Conversions (and the decimal point/comma handling or rounding issues not documented there) usually kill you and are the most important reason to avoid Variant operations in Delphi.
–jeroen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Delphi "WAT?!" of the day: | |
| var | |
| v1, v2: Variant; | |
| begin | |
| v1 := True; | |
| v2 := True; | |
| Writeln(v1 + v2); // -2 | |
| Readln; | |
| end. |






mesignapk said
I wanna learn Coding. From where I should start?? I am beginner and currently running a blog https://geekolive.com
jpluimers said
It does not matter where you start. Just start.
Arnaud Bouchez said
In fact, it is as expected. For variants, boolean are mapped as a COM Boolean, i.e. WordBool and not a pascal/Delphi boolean, with 0 or $ffff as values.
So, with sign extension, the result is as expected.
And note that FPC will give the very same results (at least in Delphi compatibility mode).
jpluimers said
Indeed. In the Variant documentation, COM is only mentioned once and OleVeriant is only at the end. It should be stressed that the stock conversions are all involving COM data types which can behave quite differently from Delphi built-in types.
Arnaud Bouchez said
To be accurate, WordBool is a true Delphi built-in type, just as native than Boolean, but in practice only used with COM/variants… :)