RAD Studio My Analize about: What's the rationale behind incosistency in Boolean casting? Ask an user on IDERA forum...

emailx45

BANNED
Регистрация
5 Май 2008
Сообщения
3,582
Реакции
2,450
Credits
578
My Analize about: What's the rationale behind incosistency in Boolean casting? Ask an user on IDERA forum...
by Emailx45 - 23/Jul/2020
[SHOWTOGROUPS=4,20]
Create an empty form, place a button and have this as the event handler:
Код:
const
  TEST_VALUE = Boolean(256);

procedure TForm1.Button1Click(Sender: TObject);
begin
  if TEST_VALUE then
    ShowMessage('The TEST_VALUE is ' + BoolToStr(TEST_VALUE, True))
end;

Only looking at the source code, what will be the correct answer:
  1. It shows the message "The TEST_VALUE is True"
  2. It does not show the message
  3. It shows the message "The TEST_VALUE is False"
Tested in Delphi 5, Delphi 2006, Delphi 10.3.3, Delphi 10.4 - all give the same result.

What's even funnier, place this piece of code inside the above event handler:
Код:
case TEST_VALUE of
    False: ShowMessage('False = ' + BoolToStr(TEST_VALUE, True));
    True: ShowMessage('True = ' + BoolToStr(TEST_VALUE, True));
  end;

What will you get?
[/SHOWTOGROUPS]


My analize:
Let's see if help:
Scenary:
  • MSWindows 10 (build 1909) 64bits
  • VCL project RAD 10.3.3 Arch 32bits
  • see "System.SysUtils.pas, line 8933 procedure VerifyBoolStrArray;" where the transformation occurr
[SHOWTOGROUPS=4,20]
Код:
unit uVCL_FormMain;

interface

uses
  Winapi.Windows,
  Winapi.Messages,
  System.SysUtils,
  System.Variants,
  System.Classes,
  Vcl.Graphics,
  Vcl.Controls,
  Vcl.Forms,
  Vcl.Dialogs,
  Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

const
  TEST_VALUE = Boolean(256); // test negative or positive values: -1000000 until 1000000 (million)

var
  lMyBoolean1: Boolean; // by default is False!
  lMyBoolean2: Boolean = False;
  lMyBoolean3: Boolean = True;

procedure TForm1.Button1Click(Sender: TObject);
begin
  {
    Value of B  Value of UseBoolStrs  Value of returned string
    true        false                 '-1'
    true        true                  The first string in TrueBoolStrs (default 'True');
    false       false                 '0'
    false       true                  The first string in FalseBoolStrs (default 'False');

    Note:
    The TrueBoolStrs and FalseBoolStrs variables are undefined when an application starts.
    Any use of StrToBool, BoolToStr, or TryStrToBool will cause these variables to be
    initialised with the default values above!
  }
  // if TEST_VALUE then { = True = value > 0 }
  begin
    // ShowMessage('The TEST_VALUE is ' + BoolToStr(TEST_VALUE, True))
    //
    Memo1.Lines.Add('False should be 0');
    Memo1.Lines.Add('True should be 1');
    Memo1.Lines.Add('-------------------######-------------------');
    Memo1.Lines.Add('Value of B Value of UseBoolStrs  Value of returned string');
    Memo1.Lines.Add('true          false                           ''-1''');
    Memo1.Lines.Add('true          true                             The first string in TrueBoolStrs (default ''True'');');
    Memo1.Lines.Add('false         false                           ''0''');
    Memo1.Lines.Add('false         true                             The first string in FalseBoolStrs (default ''False'')');
    Memo1.Lines.Add('-------------------######-------------------');
    Memo1.Lines.Add('var lMyBoolean1: Boolean; // by default is "False"');
    Memo1.Lines.Add('var lMyBoolean2: Boolean = False;');
    Memo1.Lines.Add('var lMyBoolean3: Boolean = True;');
    Memo1.Lines.Add('');
    Memo1.Lines.Add(Format('lMyBoolean1.ToString(True)  = %s', [lMyBoolean1.ToString(True)]));
    Memo1.Lines.Add(Format('lMyBoolean1.ToString(False) = %s', [lMyBoolean1.ToString(False)]));
    Memo1.Lines.Add(Format('lMyBoolean2.ToString(True)  = %s', [lMyBoolean2.ToString(False)]));
    Memo1.Lines.Add(Format('lMyBoolean2.ToString(False) = %s', [lMyBoolean2.ToString(True)]));
    Memo1.Lines.Add(Format('lMyBoolean3.ToString(True)  = %s', [lMyBoolean3.ToString(False)]));
    Memo1.Lines.Add(Format('lMyBoolean3.ToString(False) = %s', [lMyBoolean3.ToString(True)]));
    Memo1.Lines.Add('-------------------######-------------------');
    Memo1.Lines.Add('');
    //
    Memo1.Lines.Add('const TEST_VALUE = Boolean(256);');
    Memo1.Lines.Add('');
    Memo1.Lines.Add(Format('Ord(TEST_VALUE) = %d', [Ord(TEST_VALUE)]));
    Memo1.Lines.Add('');
    //
    Memo1.Lines.Add('-------------------*******-------------------');
    Memo1.Lines.Add('class function ...');
    Memo1.Lines.Add('...ToString(const Value: Boolean; UseBoolStrs: TUseBoolStrs = TUseBoolStrs.False):');
    Memo1.Lines.Add('...string; overload; inline; static;');
    Memo1.Lines.Add('-------------------*******-------------------');
    //
    Memo1.Lines.Add(Format('Boolean(Ord(TEST_VALUE)).ToString(True) = %s', [Boolean(Ord(TEST_VALUE)).ToString(True)]));
    Memo1.Lines.Add(Format('Boolean(Ord(TEST_VALUE)).ToString(False) = %s', [Boolean(Ord(TEST_VALUE)).ToString(False)]));
    Memo1.Lines.Add('');
    Memo1.Lines.Add(Format('TEST_VALUE.ToString(True) = %s', [TEST_VALUE.ToString(True)]));
    Memo1.Lines.Add(Format('TEST_VALUE.ToString(False) = %s', [TEST_VALUE.ToString(False)]));
    Memo1.Lines.Add('');
    Memo1.Lines.Add(Format('BoolToStr(TEST_VALUE, True) = %s', [BoolToStr(TEST_VALUE, True)]));
    Memo1.Lines.Add(Format('BoolToStr(TEST_VALUE, False) = %s', [BoolToStr(TEST_VALUE, False)]));

  end;
  //
  // ShowMessage( Integer(Boolean(256)).toString); //256
  // ShowMessage( Integer(TEST_VALUE).toString);  //256
  (*
    case TEST_VALUE.ToInteger of  {will be False}
    Integer(false):
    ShowMessage('False = ' + BoolToStr(TEST_VALUE, True));
    Integer(true):
    ShowMessage('True = ' + BoolToStr(TEST_VALUE, True));
    end;
  *)
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if lMyBoolean1 then
    ShowMessage('The TEST_VALUE is ' + BoolToStr(TEST_VALUE, True))
  else
    ShowMessage('lMyBoolean1 by default is False');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Self.Height := Screen.Height;
end;

end.


As in Math:
  • + and + = +
  • + and - = -
  • - and + = -
  • - and - = +









[/SHOWTOGROUPS]