Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Delphi 10.1 - Full Boolean Evaluation = False does not work.


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 5 - Last Post: Aug 17, 2017 12:47 AM Last Post By: Roy Lambert
Mauricio Gomes

Posts: 1
Registered: 11/25/11
Delphi 10.1 - Full Boolean Evaluation = False does not work.  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 16, 2017 5:46 AM
Delphi 10.1 Update 2 - Setting Full Boolean Evaluation compiler option to False is not working.

The If statements in the code below should be equivalent and Caption set to 'False2'.
But exception is thrown instead.
Is this an IDE or a compiler bug ?

Steps to reproduce: Create a VCL forms application, put a button in the form and copy the code below on OnClick event.

procedure TForm1.Button1Click(Sender: TObject);
var
  B1, B2: Boolean;
  V: Variant;
begin
  V := 'Test';
  B1 := True;
  B2 := False;
 
  if (B1) then
    if (B2) then
    begin
      if ((not V)) then
        Caption := 'True'
      else
        Caption := 'False';
    end
    else
     Caption := 'False';
 
  if (B1 and B2 and (not V)) then
    Caption := 'True2'
  else
    Caption := 'False2';
end;


Thanks.

Edited by: Mauricio Gomes on Aug 16, 2017 5:48 AM
Roy Lambert

Posts: 1,063
Registered: 8/7/01
Re: Delphi 10.1 - Full Boolean Evaluation = False does not work. [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 16, 2017 6:48 AM   in response to: Mauricio Gomes in response to: Mauricio Gomes
Mauricio

I'm still on D2006 so I can't be certain but I'd say its doing exactly what it should.

You define V as a variant then set it to 'Test';. Later on you test

if ((not V)) then

this is essentially

if ((not 'Test')) then

I'd expect exactly what I got - an exception


Debugger Exception Notification

Project Project1.exe raised exception class EVariantTypeCastError with message 'Could not convert variant of type (String) into type (Boolean)'.

Break Continue Help

Roy Lambert

Lajos Juhasz

Posts: 801
Registered: 3/14/14
Re: Delphi 10.1 - Full Boolean Evaluation = False does not work. [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 16, 2017 7:28 AM   in response to: Mauricio Gomes in response to: Mauricio Gomes
Mauricio Gomes wrote:

Delphi 10.1 Update 2 - Setting Full Boolean Evaluation compiler
option to False is not working.

[I've removed the example from the original post]

I get a different error message:
Project Project1.exe raised exception class EVariantTypeCastError with
message 'Could not convert variant of type (UnicodeString) into type
(Boolean)'.

For some reason the compiler at the line first tries the evaluate not V
and that fails:

if (B1 and B2 and (not V)) then

I've tried also to write

if (B1 and B2) and (V) then

In this case there is no error message and get the expected result. For
some reason the code generator will first pick to evaluate not V.
That cannot be classified as a bug since the meaning of the statement
is not altered in any way just not what you expect.

I've tried a bit modified test case:

function MyFunction: boolean;
begin
  ShowMessage('My Function');
  result:=true;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var
  B1, B2: Boolean;
 
begin
  B1 := True;
  B2 := False;
 
  if B1 and B2 and MyFunction then
    Caption := 'True2'
  else
    Caption := 'False2';
end;


In this case, as expected, the MyFunction is never called.
Roy Lambert

Posts: 1,063
Registered: 8/7/01
Re: Delphi 10.1 - Full Boolean Evaluation = False does not work. [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 16, 2017 8:07 AM   in response to: Lajos Juhasz in response to: Lajos Juhasz
Lajos / Mauricio

Playing a bit it looks as though its a simple precedence thing.

Look at

http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/devcommon/expressions_xml.html

not comes above and / or and is therefore evaluated first.

Roy Lambert

Lajos Juhasz

Posts: 801
Registered: 3/14/14
Re: Delphi 10.1 - Full Boolean Evaluation = False does not work. [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 16, 2017 8:20 AM   in response to: Roy Lambert in response to: Roy Lambert
Roy Lambert wrote:

Lajos / Mauricio

Playing a bit it looks as though its a simple precedence thing.

Look at

http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/devcommon/expressions_xml.html

not comes above and / or and is therefore evaluated first.

Roy Lambert

Not really as:

(A and B) and (not C)

there is no rule to evaluate first not C. As I wrote it's not a bug
either as

(A and B) and (not C) = (not C) and (A and B)

Just someone could expect that Delphi will convert

(A and B) and (not C) to:

if A and B then
  if not C then
    ....

In case of variant it first evaluates not Variant that's it.

Roy Lambert

Posts: 1,063
Registered: 8/7/01
Re: Delphi 10.1 - Full Boolean Evaluation = False does not work. [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 17, 2017 12:47 AM   in response to: Lajos Juhasz in response to: Lajos Juhasz
Lajos

I have to disagree, but I'll be interested in an official answer.

Roy Lambert

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02