Sign in Join
Wiki copytemporary

$boolEval: what changed

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

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.
1+The Free Pascal compiler [[local compiler directives|compiler directive]] `{$boolEval}` determines, whether short-circuit evaluation (also known as “lazy evaluation”) of [[Boolean]] expressions is performed.
82
93## Explanation
104
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.
5+[[Standard Pascal|Pascal]] defines, that terms of logical expressions are always evaluated from start to finish. However, in certain cases of expressions containing [[And|and]] or [[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.
426
437The following two examples are semantically identical:
448
459``` pascal
4610{$push}
4711{$boolEval off} // enable lazy evaluation
4812if itIsLate(now) and isTired(me) then
4913{$pop}
5014begin
5115 brushTeeth;
5216 goToBed;
5317end;
5418```
5519
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:
20+But this requires the reader to *know* `isTired` is possibly not called, if `itIsLate` returned [[false and true|false]]. Such implicit “removal” of code, that the order one writes expressions in is relevant, is depreciated by Pascal. Instead one writes:
7521
7622``` pascal
7723if itIsLate(now) then
7824begin
7925 if isTired(me) then
8026 begin
8127 brushTeeth;
8228 goToBed;
8329 end;
8430end;
8531```
8632
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.
33+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.
10034
10135## See also
10236
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.
37+- [`{$B}` or `{$boolEval}`: complete Boolean evaluation in “Free Pascal programmer's guide”](https://www.freepascal.org/docs-html/prog/progsu4.html)
38+- [`math.ifThen`](https://www.freepascal.org/docs-html/rtl/math/ifthen.html), the mock ternary operator that always computes both possible results.
12239
12340---
12441*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).*
12542Category: Local compiler directives

Back to history