Sign in Join

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

Mailing lists / fpc-pascal / 2005-11

Boolean expression short-circuit

5 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

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

Kornel Kisielewicz

Ok, I've been arguing over that with a friend of mine for a couple of e-mail's now... I need your help!

Let me show an example first:

type TFoo = class end;
var  Foo : TFoo;

The question is, wether this is valid:

if (Foo  nil) and (Foo.inheritsFrom(TFoo)) then do_something;

I would have no questions about that (first the Foo is checked, so inheritsFrom is called ONLY if Foo nil) if it weren't for the statement in the manual:

<quote>
Remark: The order in which expressions of the same precedence are evaluated is not guaranteed to be leftto-right. In general, no assumptions on which expression is evaluated first should be made in such a case. The compiler will decide which expression to evaluate first based on optimization rules.
</quote>

Can I make the assumption that boolean expressions are GUARANTEED to be evaluated left-to-right?

regards, and thank you,

At your service, Kornel Kisielewicz [http://chaos.magma-net.pl/doom] "Some thing's just cannot be programmed in a roguelike. For everything else, there's GenRogue" -- Anubis
Michalis Kamburelis
to Kornel Kisielewicz
Kornel Kisielewicz wrote 24 lines
Ok, I've been arguing over that with a friend of mine for a couple of e-mail's now... I need your help! Let me show an example first: type TFoo = class end; var Foo : TFoo; The question is, wether this is valid: if (Foo nil) and (Foo.inheritsFrom(TFoo)) then do_something; I would have no questions about that (first the Foo is checked, so inheritsFrom is called ONLY if Foo nil) if it weren't for the statement in the manual: <quote> Remark: The order in which expressions of the same precedence are evaluated is not guaranteed to be leftto-right. In general, no assumptions on which expression is evaluated first should be made in such a case. The compiler will decide which expression to evaluate first based on optimization rules. </quote>

This concerns +, -, * and such operators. Compiler can rearrange your arithmetical statements to make them optimized. Both Delphi and FPC.

This *doesn't* concern boolean operators, like "and", "or" -- these are guaranteed to be calculated left-to-right. That's because we like them to be predictable, just like your example "if (Foo nil) and (Foo..." demonstrates.

Can I make the assumption that boolean expressions are GUARANTEED to be evaluated left-to-right?

Yes.

Michalis

Kornel Kisielewicz
to Michalis Kamburelis
Michalis Kamburelis wrote
> Can I make the assumption that boolean expressions are GUARANTEED to > be evaluated left-to-right? Yes.

Thanks, I guess I lost the argument :]. Anyway, I guess it wouldn't hurt to clarify that just a little bit in the manual :-/

At your service, Kornel Kisielewicz (charonATmagma-net.pl) [http://chaos.magma-net.pl] "Come on, Kornel. 11 years and no binary? And it's not vapourware?" -- Mike Blackney
Matt Emson
to Kornel Kisielewicz
Thanks, I guess I lost the argument :]. Anyway, I guess it wouldn't hurt to clarify that just a little bit in the manual :-/

However, IIRC "complete boolean eval" (which is what delphi calls it) would blow the code up, as even if Foo was nil, the second half would be evaluated regardless.

Maybe that will go some way to winning part of the argument? ;-)

Michael Van Canneyt
to Kornel Kisielewicz: Thanks, I guess I lost the argument :]. Anyway, I guess it wouldn't hurt to clarify that…
Kornel Kisielewicz wrote 8 lines
Michalis Kamburelis wrote: >> Can I make the assumption that boolean expressions are GUARANTEED to be >> evaluated left-to-right? > > Yes. Thanks, I guess I lost the argument :]. Anyway, I guess it wouldn't hurt to clarify that just a little bit in the manual :-/

Boolean expression evaluation is explicitly treated in both reference manual and programmers reference.

However, I'll add an explicit remark about the consequence this has for the order of evaluation, namely that the order is guaranteed left-to-right. (although I think this is a logical consequence of the shortcut boolean evaluation principle)

Michael.