Inconsistent behaviour with short boolean evaluation !!!
2 views
Skip to first unread message
Skybuck Flying
unread,
Feb 11, 2015, 7:59:48 PM2/11/15
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Sign in to report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to
Hello,
Delphi XE 7 behaves inconsistent concerning function result "build up":
// CASE 1 (AND): will always call all functions !
function Example1 : boolean;
begin
result := True;
result := result and FunctionA;
result := result and FunctionB;
result := result and FunctionC;
end;
// CASE 2 (OR): short boolean evaluation makes this code fail ! once
functionA returns true, the other 2 functions will not called !
function Example2 : boolean;
begin
result := False;
result := result or FunctionA;
result := result or FunctionB;
result := result or FunctionC;
end;
// This kind of inconsistent behaviour is bad !!!
// Recommendation: Always execute functions which are called on "boolean
chains !"
Bye,
Skybuck.
Skybuck Flying
unread,
Feb 11, 2015, 8:07:43 PM2/11/15
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Sign in to report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to
For now I solve this problem as follows:
function Example2 : boolean;
var
vResult : array[0..12] of boolean;
vIndex : integer;
begin
result := false;
I know that compiler option exists but turning off short evaluation is not
good enough, because I do want short evaluation in all other cases.
I don't want short evaluation when functions are involved. That's all... so
I hope to see improvements in Delphi XE 7.
I also consider it a bit of a bug... because those functions may actually do
something usefull... simply "disabling" something usefull just basic some
other "boolean logic" part can be short-circuited doesn't really make sense.
Bye,
Skybuck.
Skybuck Flying
unread,
Feb 12, 2015, 11:19:47 AM2/12/15
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Sign in to report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to
Then again... one could argue that just the mere "act" of reading some
boolean could also trigger something.
Like an access violation.
In that case my desired behaviour would introduce a new inconsistency.
If functions were executed they could access violate, but the booleans
themselfes not.
Perhaps it's best to leave Delphi XE 7 the way it is.
I will have to live with writing around it.
Or maybe some other solution exists... perhaps indicating somehow that the
functions must be fully evaluated when on "boolean chains".
function test : boolean; no_short_circuit;
function test : boolean; full_boolean_evaluation; // or this.
begin
end;
result := result or test;
^ test always executes.
Bye,
Skybuck.
Grinder
unread,
Feb 12, 2015, 5:35:59 PM2/12/15
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Sign in to report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message