$boolEval: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | - | ||
| 2 | - | ||
| 3 | - | ||
| 4 | - | ||
| 5 | - | ||
| 6 | - | ||
| 7 | - | ||
| 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. | |
| 8 | 2 | ||
| 9 | 3 | ## Explanation | |
| 10 | 4 | ||
| 11 | - | ||
| 12 | - | ||
| 13 | - | ||
| 14 | - | ||
| 15 | - | ||
| 16 | - | ||
| 17 | - | ||
| 18 | - | ||
| 19 | - | ||
| 20 | - | ||
| 21 | - | ||
| 22 | - | ||
| 23 | - | ||
| 24 | - | ||
| 25 | - | ||
| 26 | - | ||
| 27 | - | ||
| 28 | - | ||
| 29 | - | ||
| 30 | - | ||
| 31 | - | ||
| 32 | - | ||
| 33 | - | ||
| 34 | - | ||
| 35 | - | ||
| 36 | - | ||
| 37 | - | ||
| 38 | - | ||
| 39 | - | ||
| 40 | - | ||
| 41 | - | ||
| 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. | |
| 42 | 6 | ||
| 43 | 7 | The following two examples are semantically identical: | |
| 44 | 8 | ||
| 45 | 9 | ``` pascal | |
| 46 | 10 | {$push} | |
| 47 | 11 | {$boolEval off} // enable lazy evaluation | |
| 48 | 12 | if itIsLate(now) and isTired(me) then | |
| 49 | 13 | {$pop} | |
| 50 | 14 | begin | |
| 51 | 15 | brushTeeth; | |
| 52 | 16 | goToBed; | |
| 53 | 17 | end; | |
| 54 | 18 | ``` | |
| 55 | 19 | ||
| 56 | - | ||
| 57 | - | ||
| 58 | - | ||
| 59 | - | ||
| 60 | - | ||
| 61 | - | ||
| 62 | - | ||
| 63 | - | ||
| 64 | - | ||
| 65 | - | ||
| 66 | - | ||
| 67 | - | ||
| 68 | - | ||
| 69 | - | ||
| 70 | - | ||
| 71 | - | ||
| 72 | - | ||
| 73 | - | ||
| 74 | - | ||
| 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: | |
| 75 | 21 | ||
| 76 | 22 | ``` pascal | |
| 77 | 23 | if itIsLate(now) then | |
| 78 | 24 | begin | |
| 79 | 25 | if isTired(me) then | |
| 80 | 26 | begin | |
| 81 | 27 | brushTeeth; | |
| 82 | 28 | goToBed; | |
| 83 | 29 | end; | |
| 84 | 30 | end; | |
| 85 | 31 | ``` | |
| 86 | 32 | ||
| 87 | - | ||
| 88 | - | ||
| 89 | - | ||
| 90 | - | ||
| 91 | - | ||
| 92 | - | ||
| 93 | - | ||
| 94 | - | ||
| 95 | - | ||
| 96 | - | ||
| 97 | - | ||
| 98 | - | ||
| 99 | - | ||
| 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. | |
| 100 | 34 | ||
| 101 | 35 | ## See also | |
| 102 | 36 | ||
| 103 | - | ||
| 104 | - | ||
| 105 | - | ||
| 106 | - | ||
| 107 | - | ||
| 108 | - | ||
| 109 | - | ||
| 110 | - | ||
| 111 | - | ||
| 112 | - | ||
| 113 | - | ||
| 114 | - | ||
| 115 | - | ||
| 116 | - | ||
| 117 | - | ||
| 118 | - | ||
| 119 | - | ||
| 120 | - | ||
| 121 | - | ||
| 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. | |
| 122 | 39 | ||
| 123 | 40 | --- | |
| 124 | 41 | *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 | 42 | Category: Local compiler directives |
