Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2019-07

Evaluation order in expressions

9 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Ondrej Pokorny

Hello,

can somebody comment on the Remark from
https://www.freepascal.org/docs-html/ref/refch12.html ? /Remark: The order in which expressions of the same precedence are evaluated is not guaranteed to be left-to-right./

Is this valid for boolean expressions, too? I use the following construct with {$BOOLEVAL OFF}:
Result := Assigned(MyObject) and (MyObject.Value>5); The remark above suggests that I cannot use it because (MyObject.Value>5) may be evaluated before Assigned(MyObject) if the optimizer decides so.

On the other hand, the information in
https://www.freepascal.org/docs-html/current/prog/progsu4.html#x11-100001.2.4 suggests the opposite:
/So, in the following example, the function Bofu, which has a boolean result, will never get called.//
//If False and Bofu then/

Delphi documentation also explicitly states that the expressions are evaluated in left to right order:
http://docwiki.embarcadero.com/RADStudio/Rio/en/Boolean_short-circuit_evaluation_(Delphi_compiler_directive)

(Well it is no surprise because
https://www.freepascal.org/docs-html/ref/refch12.html states that "/This behaviour is distinctly different from Delphi or Turbo Pascal./")

Ondrej

Reply by email

Ondrej Pokorny
to Ondrej Pokorny
Ondrej Pokorny wrote 7 lines
/Remark: The order in which expressions of the same precedence are evaluated is not guaranteed to be left-to-right./ Is this valid for boolean expressions, too? I use the following construct with {$BOOLEVAL OFF}: Result := Assigned(MyObject) and (MyObject.Value>5);

A moment of relief. The FPC compiler itself depends on the left-to-right evaluation. E.g. compiler/aoptbase.pas, line 284:

    while assigned(p1) and assigned(p2) and GetNextInstruction(p1,p1) and (p1p2) do

So is the documentation wrong or are the compiler (and also my own) sources flawed?

Best
Ondrej

Reply by email

Michael Van Canneyt
to Ondrej Pokorny
Ondrej Pokorny wrote 17 lines
On 30.07.2019 08:23, Ondrej Pokorny wrote: > /Remark: The order in which expressions of the same precedence are > evaluated is not guaranteed to be left-to-right./ > > Is this valid for boolean expressions, too? I use the following construct > with {$BOOLEVAL OFF}: > Result := Assigned(MyObject) and (MyObject.Value>5); > A moment of relief. The FPC compiler itself depends on the left-to-right evaluation. E.g. compiler/aoptbase.pas, line 284:     while assigned(p1) and assigned(p2) and GetNextInstruction(p1,p1) and (p1p2) do So is the documentation wrong or are the compiler (and also my own) sources flawed?

The documentation has already been fixed in this regard, there was a bugreport about it not so long ago.

Michael.

Reply by email

Ondrej Pokorny
to Michael Van Canneyt
Michael Van Canneyt wrote
The documentation has already been fixed in this regard, there was a bugreport about it not so long ago.

Great, thank you. Is there some online version of the latest documentation trunk (nightly build)?

Ondrej

Reply by email

Jonas Maebe
to Ondrej Pokorny: can somebody comment on the Remark from https://www.freepascal.org/docs-html/ref/refch12.h…
Ondrej Pokorny wrote 6 lines
can somebody comment on the Remark from https://www.freepascal.org/docs-html/ref/refch12.html ? _Remark: The order in which expressions of the same precedence are evaluated is not guaranteed to be left-to-right._ Is this valid for boolean expressions, too?

Only if short circuit boolean evaluation is off.

(Well it is no surprise because https://www.freepascal.org/docs-html/ref/refch12.html states that "_This behaviour is distinctly different from Delphi or Turbo Pascal._")

Delphi evaluates e.g. all parameters to subroutine calls from left to right, which FPC does not guarantee.

Jonas

Reply by email

Ondrej Pokorny
to Jonas Maebe: Only if short circuit boolean evaluation is off. Delphi evaluates e.g. all parameters to…
Jonas Maebe wrote
Delphi evaluates e.g. all parameters to subroutine calls from left to right, which FPC does not guarantee.

If I am not mistaken Delphi doesn't guarantee this either. I remember I "learned it by doing" quite some time ago and I don't think I used FPC at that point, so it must have been Delphi.

A quick Google search revealed https://stackoverflow.com/a/11010685 - yes, when I run the code from the answer in Delphi 32bit the right "SideEffect(2)" call is executed before the left "SideEffect(1)" call.

Ondrej

Reply by email

Michael Van Canneyt
to Ondrej Pokorny
Ondrej Pokorny wrote 11 lines
On 30.07.2019 08:55, Jonas Maebe wrote: > Delphi evaluates e.g. all parameters to subroutine calls from left to > right, which FPC does not guarantee. If I am not mistaken Delphi doesn't guarantee this either. I remember I "learned it by doing" quite some time ago and I don't think I used FPC at that point, so it must have been Delphi. A quick Google search revealed https://stackoverflow.com/a/11010685 - yes, when I run the code from the answer in Delphi 32bit the right "SideEffect(2)" call is executed before the left "SideEffect(1)" call.

I think they updated the documentation meanwhile:

http://docwiki.embarcadero.com/RADStudio/Rio/en/Procedures_and_Functions_(Delphi)#Calling_Conventions

So you can see it depends on the calling convention.

Michael.

Reply by email