Sign in Join
Wiki copytemporary

$boolEval

The Free Pascal compiler compiler directive {$boolEval} determines, whether short-circuit evaluation (also known as “lazy evaluation”) of Boolean expressions is performed.

Explanation

Pascal defines, that terms of logical expressions are always evaluated from start to finish. However, in certain cases of expressions containing and or or further evaluation can be skipped, since the final result can be deduced from the intermediate result, thus saving computation time. This is similar to shell’s && and ||. But Pascal consciously decided against such behavior. Ratio: Expressions can consist of function calls, those returning a boolean value, and if their definitions trigger side-effects a short-circuit evaluation could prevent such being executed. That is contrary to Pascal’s paradigm, that the programmer is not required to know of (implicit) optimizations (that actually change a program’s behavior), but what she writes is what is being executed. The order one writes terms in is not supposed to matter, since $\land$ and $\lor$ are associative operators, too.

The following two examples are semantically identical:

pascal
{$push}
{$boolEval off} // enable lazy evaluation
if itIsLate(now) and isTired(me) then
{$pop}
begin
    brushTeeth;
    goToBed;
end;

But this requires the reader to know isTired is possibly not called, if itIsLate returned false. Such implicit “removal” of code, that the order one writes expressions in is relevant, is depreciated by Pascal. Instead one writes:

pascal
if itIsLate(now) then
begin
    if isTired(me) then
    begin
        brushTeeth;
        goToBed;
    end;
end;

By default the FPC uses shortcut Boolean evaluation. Extended Pascal, which as of 2020 FPC plans to support one day, defines the additional logical operators and_then and or_else which do exactly this, lazy evaluation, but on the explicit request of the programmer.

See also


From the Free Pascal wiki, page "$boolEval", as it stood on 2020-06-23 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 2, last changed by WikiArchive .

Edited on wiki.freepascal.org by 2 people, 2018–2020

From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.

WhenWhoWhat changed
Kai Burghardtcreate
TrevFixed template loop; categorised page (minor)
Kai Burghardtsubstitute legacy syntaxhighlight syntax; update date (2018 → 2020) for statement still being true; typography; link FPC again, as that was removed in previous edit
Kai Burghardtset more specific Local compiler directives

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.