[Estimated Reading Time: < 1 minutes]

My most recent posts have prompted a bit of discussion, and it seems some concern, regarding the implementation of Boolean values in Delphi. The concern at least I think is unwarranted, as long as you avoid explicitly comparing a Boolean value to the True constant and allow the compiler to logically evaluate the Boolean itself. But in the follow up investigations of one commenter (thank you Arthur), a further occurrence of the alternate -1 identity of True has been identified: Variants.

Arthur posted the following demonstration that passing a Boolean True through an intermediate Variant variable would transmute the Delphi True with ordinality 1 into an integer with ordinality -1:

procedure TForm1.Button1Click(Sender: TObject);
var aVariant:Variant; 
      b:boolean; 
      i:integer;
begin
  b:=True;

  i:=integer(b); //Cast Bool to Int
  Showmessage(format('In Delphi, TRUE is %d',[i]));

  aVariant:=b; // boolean -> variant
  i:=aVariant; // variant -> Int

  Showmessage(format('True is now suddenly %d',[i]));
end;

The thing to remember about Variant is that it was born in COM at a time when VB was the dominant language in that space. As Raymond Chen explains, it was the VB/BASIC definition of Boolean that largely determined the COM Variant implementation. So a Variant Boolean with the value True has the ordinal value VARIANT_TRUE.

Or -1.

Of course, this isn’t actually an explanation of why -1 was chosen in BASIC, only why the BASIC definition was subsequently adopted in COM/Variant. The underlying rationale for the use of -1 at all is I think is most readily explained by the bit-flipping, logical negation of 0.

And as is also apparent from Raymond’s post, the apparent schizophrenia of True is far from being unique to Delphi.

6 thoughts on “The Varying (and Variant) Value of True”

    1. Not False is indeed True and also true, but there are also 2^N – 2 values (for N-bit size True) which are true but not True and yet not Not False. On the other hand, there is only one true True which is by definition not False but which is also not in fact Not False. 😀

      Will there be anything else minister ? 🙂

Comments are closed.