mercredi 28 novembre 2018

How does this if not statement work without parentheses?

When creating a new Windows Service in Delphi, it inserts the following:

if not Application.DelayInitialize or Application.Installing then
  Application.Initialize;

The author didn't bother including parentheses, so I'm trying to wrap my head around this. Only the first part of the statement has not.

I can see this possibly meaning two things:

if not (Application.DelayInitialize or Application.Installing) then

or

if (not Application.DelayInitialize) or Application.Installing then

Based on the context of the particular code, I would expect the first scenario to apply, considering if a service is starting, you don't want it to initialize, but do want it to initialize if it's not installing. However, my testing proved the opposite. At least that's what I've gathered based on the following test:

program TestIfNot;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils;

var
  Bool1, Bool2: Boolean;

begin
  try
    WriteLn('if not Bool1 or Bool2 then True else False');

    WriteLn('1) False, False');
    Bool1:= False;
    Bool2:= False;
    if not Bool1 or Bool2 then
      WriteLn('True')
    else
      WriteLn('False');

    WriteLn('2) True, False');
    Bool1:= True;
    Bool2:= False;
    if not Bool1 or Bool2 then
      WriteLn('True')
    else
      WriteLn('False');

    WriteLn('3) False, True');
    Bool1:= False;
    Bool2:= True;
    if not Bool1 or Bool2 then
      WriteLn('True')
    else
      WriteLn('False');

    WriteLn('4) True, True');
    Bool1:= True;
    Bool2:= True;
    if not Bool1 or Bool2 then
      WriteLn('True')
    else
      WriteLn('False');

    WriteLn;



    WriteLn('if not (Bool1 or Bool2) then True else False');

    WriteLn('1) False, False');
    Bool1:= False;
    Bool2:= False;
    if not (Bool1 or Bool2) then
      WriteLn('True')
    else
      WriteLn('False');

    WriteLn('2) True, False');
    Bool1:= True;
    Bool2:= False;
    if not (Bool1 or Bool2) then
      WriteLn('True')
    else
      WriteLn('False');

    WriteLn('3) False, True');
    Bool1:= False;
    Bool2:= True;
    if not (Bool1 or Bool2) then
      WriteLn('True')
    else
      WriteLn('False');

    WriteLn('4) True, True');
    Bool1:= True;
    Bool2:= True;
    if not (Bool1 or Bool2) then
      WriteLn('True')
    else
      WriteLn('False');

    WriteLn;



    WriteLn('if (not Bool1) or Bool2 then True else False');

    WriteLn('1) False, False');
    Bool1:= False;
    Bool2:= False;
    if (not Bool1) or Bool2 then
      WriteLn('True')
    else
      WriteLn('False');

    WriteLn('2) True, False');
    Bool1:= True;
    Bool2:= False;
    if (not Bool1) or Bool2 then
      WriteLn('True')
    else
      WriteLn('False');

    WriteLn('3) False, True');
    Bool1:= False;
    Bool2:= True;
    if (not Bool1) or Bool2 then
      WriteLn('True')
    else
      WriteLn('False');

    WriteLn('4) True, True');
    Bool1:= True;
    Bool2:= True;
    if (not Bool1) or Bool2 then
      WriteLn('True')
    else
      WriteLn('False');




    ReadLn;


  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

These are the results:

if not Bool1 or Bool2 then True else False
1) False, False
True
2) True, False
False
3) False, True
True
4) True, True
True

if not (Bool1 or Bool2) then True else False
1) False, False
True
2) True, False
False
3) False, True
False
4) True, True
False

if (not Bool1) or Bool2 then True else False
1) False, False
True
2) True, False
False
3) False, True
True
4) True, True
True

The first block shows the original line in question, the second block shows what I expected to be the case, but the third block shows the same result as the first, suggesting that

if not Application.DelayInitialize or Application.Installing then

is the same as

if (not Application.DelayInitialize) or Application.Installing then

And in the context of the service app, it seems as if it's the opposite - it would initialize if it's being installed (opposite of what I thought).

Can someone give me some clarification what I'm looking at here?

Aucun commentaire:

Enregistrer un commentaire