View Issue Details

IDProjectCategoryView StatusLast Update
0035661FPCDocumentationpublic2019-06-03 06:44
ReporterZoran Vučenović Assigned ToMichael Van Canneyt  
Status resolvedResolutionfixed 
Fixed in Version3.3.1 
Summary0035661: Expressions - order of evaluation
DescriptionTake a look at these two quotes from Refernce manual:

Quote 1 (Under Boolean operators --

Remark: By default, boolean expressions are evaluated with short-circuit evaluation. This means that from the moment the result of the complete expression is known, evaluation is stopped and the result is returned. For instance, in the following expression:
 B := True or MaybeTrue;
The compiler will never look at the value of MaybeTrue, since it is obvious that the expression will always be True. As a result of this strategy, if MaybeTrue is a function, it will not get called ! (This can have surprising effects when used in conjunction with properties)

Quote 2 (chapter Expressions, when talking about operator precedence --

Remark: The order in which expressions of the same precedence are evaluated is not guaranteed to be left-to-right. In general, no assumptions on which expression is evaluated first should be made in such a case.
... etc.

So, according to Qoute 2, in the example from Quote 1, compiler can choose to first evaluate the right side of OR operator. Then, we cannot rely on short-circuit evaluation.

I need to say that I often use something like "if (Obj <> nil) and Obj.SomeMethod do..." and after many years of programming with FPC, and using short-circuit evaluation all the time, I never encountered any bad surprises.
Therefore, I think that it actually always evaluates left side first, at least when short-circuit evaluation is switched on.

The documentation should clear this. I think that it should be mentioned clearly in both remarks I quoted:
- in boolean operators, that boolean operators are excluded from general rule, at least when short-circuit evaluation is enabled, and the order left to right is guaranteed in this case.
- in Expressions, it should be added to quoted remark, that this remark doesn't include boolean operators (at least when short-circuit evaluation is enabled) and that these are guaranteed to be evaluated from left to right.
TagsNo tags attached.
Fixed in Revision1621.
Attached Files


Michael Van Canneyt

2019-06-03 06:44

administrator   ~0116546

I have added a paragraph to the 'order of evaluation' section.

I do not think it is necessary to add something to the short-circuit evaluation part.

Thanks for pointing it out !

Issue History

Date Modified Username Field Change
2019-05-31 18:21 Zoran Vučenović New Issue
2019-05-31 18:21 Zoran Vučenović Status new => assigned
2019-05-31 18:21 Zoran Vučenović Assigned To => Michael Van Canneyt
2019-06-03 06:44 Michael Van Canneyt Status assigned => resolved
2019-06-03 06:44 Michael Van Canneyt Resolution open => fixed
2019-06-03 06:44 Michael Van Canneyt Fixed in Version => 3.3.1
2019-06-03 06:44 Michael Van Canneyt Fixed in Revision => 1621.
2019-06-03 06:44 Michael Van Canneyt FPCTarget => 3.2.0
2019-06-03 06:44 Michael Van Canneyt Note Added: 0116546