Boolean expression short-circuit

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Boolean expression short-circuit

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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Boolean expression short-circuit

Michalis Kamburelis
Kornel Kisielewicz wrote:

> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Boolean expression short-circuit

Kornel Kisielewicz
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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Boolean expression short-circuit

Matt Emson
> 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? ;-)

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Boolean expression short-circuit

Michael Van Canneyt
In reply to this post by Kornel Kisielewicz


On Tue, 22 Nov 2005, Kornel Kisielewicz wrote:

> 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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal