Forum > General
and operator order
scribly:
Hi, a long time ago I programmed in Delphi and there the AND operator would get evaluated in a willy nilly order.
E.g:
--- Code: ---var x: TSomeClass; //(it's initialized when used)
function getObjectX(var _x: TSomeClass): boolean;
_x:=x;
return true
end
...
if getObjectX(x) and x.dosomethingthatreturnsaboolean() then ...
--- End code ---
this would randomly crash as x would sometimes be uninitialized
Is this still the case in modern freepascal? (I still make sure my code splits this up into separate lines but wondering if I still have to)
Zvoni:
--- Quote from: scribly on November 11, 2024, 02:16:39 pm ---Hi, a long time ago I programmed in Delphi and there the AND operator would get evaluated in a willy nilly order.
E.g:
--- Code: ---var x: TSomeClass; //(it's initialized when used)
function getObjectX(var _x: TSomeClass): boolean;
_x:=x;
return true
end
...
if getObjectX(x) and x.dosomethingthatreturnsaboolean() then ...
--- End code ---
this would randomly crash as x would sometimes be uninitialized
Is this still the case in modern freepascal? (I still make sure my code splits this up into separate lines but wondering if I still have to)
--- End quote ---
If GetObjectx(x) returns False, the second part of the "and" DOES NOT get evaluated.
For Debugging-purposes i'd rather use
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var a,b:Boolean;....a:=GetObjectx(x);If a Then b:=x.dosomethingthatreturnsaboolean();
This way i'm protecting myself in case a is False
Warfley:
There is the $BoolEval which does configure the behavior of boolean operators. By default it is disabled, resulting in so called short circuit evalutation. So:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if A and B then ...is basically translated to
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if A then if B then ...So if A is false, the evaluation of B never starts.
If you manually enable it, e.g. With {$B+}, then all the expressions will be evaluated. Then the order of evaluation (associativity as it is called for operators) is, as with any operators in Pascal undefined.
PascalDragon:
--- Quote from: scribly on November 11, 2024, 02:16:39 pm ---Hi, a long time ago I programmed in Delphi and there the AND operator would get evaluated in a willy nilly order.
--- End quote ---
And what Warfley said also applies to Delphi, even “a long time ago”.
MarkMLl:
--- Quote from: PascalDragon on November 11, 2024, 07:29:19 pm ---
--- Quote from: scribly on November 11, 2024, 02:16:39 pm ---Hi, a long time ago I programmed in Delphi and there the AND operator would get evaluated in a willy nilly order.
--- End quote ---
And what Warfley said also applies to Delphi, even “a long time ago”.
--- End quote ---
And to Turbo Pascal, and even unto ALGOL-60.
Boolean expressions in conditional statements (if, while, until) are fundamentally different from arithmetic expressions. In effect, they're evaluated into nested if-then-else statements rather than being evaluated in a single calculation.
MarkMLl
Navigation
[0] Message Index
[#] Next page