Name

$BoolEval Compiler Directive

Syntax

{$B-}          // default
{$BoolEval Off} // default
{$B+}
{$BoolEval On}

Scope

Local

Description

By default, the and and or operators do not evaluate their right-hand operands if the expression result is known from the left-hand operand. This is known as short-circuiting the expression. C, C++, and Java programmers are familiar with short-circuit logical operators.

If you prefer the traditional Pascal use of the and and or operators, you can disable short-circuit operators with the $BoolEval compiler directive. Note that most Delphi programmers prefer using short-circuit operators because they produce code that is easier to read.

Tip

Most Delphi programs work correctly only when $BoolEval is disabled. Do not enable this option unless you know the code does not rely on short-circuit operators.

Example

// Ensure that a path ends with a backslash.
// The first approach uses the short-circuit AND operator.
{$BoolEval Off}
if (Length(Path) > 0) and (Path[Length(Path)] <> '\') then
  Path := Path + '\';

// This is how you must write the same expression without
// using the short-circuit operator.
{$BoolEval On}
if Length(Path) > 0 then
  if Path[Length(Path)] <> '\' then
    Path := Path + '\';

See Also

And Keyword, Boolean Type, Or Keyword

Get Delphi in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.