このコードで関数anotherfunc()
が呼び出された場合
if (somefunc() = False and anotherfunc() = True) then
次に、を設定しましたBOOLEVAL ON
Davidが指摘したように、コンパイラは最初に評価しますFalse and anotherfunc()
BOOLEVAL OFF
モードでは、コンパイラーはそれをFalse and AnyBoolState
認識しているため、呼び出されないFalse
ため、anotherfunc()
呼び出されません(実際には呼び出されません)。
その簡単なテストとして、私はあなたの表現を示すためにjachaguateプログラムを拡張しました
program AndEvaluation;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
function FalseFunc( const AName : string ) : Boolean;
begin
Write( AName, '(False)', '-' );
Result := False;
end;
function TrueFunc( const AName : string ) : Boolean;
begin
Write( AName, '(True)', '-' );
Result := True;
end;
begin
try
// (somefunc() = False and anotherfunc() = True)
//
// in this testcase translated to:
//
// somefunc() => FalseFunc( 'First' )
// False => FalseFunc( 'Second' )
// anotherfunc() => TrueFunc( 'Third' )
// True => TrueFunc( 'Fourth' )
{$B+}
Writeln( 'BOOLEVAL ON' );
if ( FalseFunc( 'First' ) = FalseFunc( 'Second' ) and TrueFunc( 'Third' ) = TrueFunc( 'Fourth' ) )
then
Writeln( 'True' )
else
Writeln( 'False' );
{$B-}
Writeln( 'BOOLEVAL OFF' );
if ( FalseFunc( 'First' ) = FalseFunc( 'Second' ) and TrueFunc( 'Third' ) = TrueFunc( 'Fourth' ) )
then
Writeln( 'True' )
else
Writeln( 'False' );
except
on E : Exception do
Writeln( E.ClassName, ': ', E.Message );
end;
ReadLn;
end.
そして今、結果を見てみましょう
BOOLEVAL ON
Second(False)-Third(True)-First(False)-Fourth(True)-True
BOOLEVAL OFF
First(False)-Second(False)-Fourth(True)-True
出力が説明しているようにBOOLEVAL ON
、あなたanotherfunc()
が前 somefunc()
に呼び出されることは呼び出されます。
あなたBOOLEVAL OFF
とanotherfunc()
は決して呼ばれません。
あなたがと同じにしたい場合
if (somefunc() == FALSE && anotherfunc() == FALSE)
あなたはそれをこのように翻訳しなければなりません
if ( somefunc() = False ) and ( anotherfunc() = False ) then
またはより良い、より短い方法
if not somefunc() and not anotherfunc() then
または多分さらに短い
if not( somefunc() or anotherfunc() ) then
anotherfunc()
ただし、設定する必要があるたびに呼び出されるのを避けるためBOOLEVAL OFF