Sign in Join
Wiki copytemporary

$boolEval: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: $boolEval

OldNewLine
1+The Free Pascal compiler [[local compiler directives|compiler directive]]
2+
3+``` pascal
4+{$boolEval}
5+```
6+
7+determines, whether short-circuit evaluation (also known as “lazy evaluation”) of [[Boolean]] expressions is performed.
8+
9+## Explanation
10+
11+[[Standard Pascal|Pascal]] defines, that terms of logical expressions are always evaluated from start to finish. However, in certain cases of expressions containing \[\[And\|
12+
13+``` pascal
14+and
15+```
16+
17+\]\] or \[\[Or\|
18+
19+``` pascal
20+or
21+```
22+
23+\]\] 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
24+
25+``` bash
26+&&
27+```
28+
29+and
30+
31+``` bash
32+||
33+```
34+
35+. But Pascal consciously decided against such behavior. Ratio: Expressions can consist of function calls, those returning a
36+
37+``` pascal
38+boolean
39+```
40+
41+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.
42+
43+The following two examples are semantically identical:
44+
45+``` pascal
46+{$push}
47+{$boolEval off} // enable lazy evaluation
48+if itIsLate(now) and isTired(me) then
49+{$pop}
50+begin
51+ brushTeeth;
52+ goToBed;
53+end;
54+```
55+
56+But this requires the reader to *know*
57+
58+``` pascal
59+isTired
60+```
61+
62+is possibly not called, if
63+
64+``` pascal
65+itIsLate
66+```
67+
68+returned \[\[false and true\|
69+
70+``` pascal
71+false
72+```
73+
74+\]\]. Such implicit “removal” of code, that the order one writes expressions in is relevant, is depreciated by Pascal. Instead one writes:
75+
76+``` pascal
77+if itIsLate(now) then
78+begin
79+ if isTired(me) then
80+ begin
81+ brushTeeth;
82+ goToBed;
83+ end;
84+end;
85+```
86+
87+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
88+
89+``` pascal
90+and_then
91+```
92+
93+and
94+
95+``` pascal
96+or_else
97+```
98+
99+which do exactly this, lazy evaluation, but on the *explicit* request of the programmer.
100+
101+## See also
102+
103+- \[<https://www.freepascal.org/docs-html/prog/progsu4.html>
104+ ``` pascal
105+ {$B}
106+ ```
107+
108+ or
109+
110+ ``` pascal
111+ {$boolEval}
112+ ```
113+
114+
115+ complete Boolean evaluation in “Free Pascal programmer's guide”\]
116+- \[<https://www.freepascal.org/docs-html/rtl/math/ifthen.html>
117+ ``` pascal
118+ math.ifThen
119+ ```
120+
121+ \], the mock ternary operator that always computes both possible results.
122+
123+---
124+*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](/wiki-contact).*
125+Category: Local compiler directives

Back to history