octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: short circuit & and | in if and while


From: John W. Eaton
Subject: Re: short circuit & and | in if and while
Date: Thu, 14 Sep 2017 00:07:27 -0400
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1

On 09/13/2017 09:25 PM, Ernst Reissner wrote:
Yes, Mike, I know the what, but not the why and I really cannot figure
out a reason.
The reason why I ask is among other things, what about until in octave:
Maybe short circuiting should be also for until, as it is for while and if:
while (false & error("both")) "hi";end shortcuts and thus no error while
do "hi" until(true |error("both")); yields and error.

This seems a little inconsistent,
but on the other hand, ... what sense makes the matlab feature at all?!

It doesn't make much sense.

Have you read

  http://wiki.octave.org/FAQ#Short-circuit_.26_and_.7C_operators

Originally in Octave && was equivalent to &, and || was equivalent to |. Neither form did short-circuiting.

I changed the && and || operators (but not the & and | operators) to do short-circuiting in 1994, apparently. Here is the message I posted then:

  http://lists.gnu.org/archive/html/help-octave/1994-05/msg00012.html

When TMW introduced short-circuit behavior for the & and | operators in Matlab, they thought they were being clever by not introducing new operators, and just making the existing ones do something different in some contexts. To me, this seemed like a really bad choice and I could immediately see cases where behavior would be different depending on whether the expression appeared directly inside and IF/WHILE condition or not.

The Matlab behavior is highly unusual and causes trouble in various ways (some documented in the Octave FAQ). We have it for compatibility ONLY. Using this feature is STRONGLY discouraged. I wish to limit the damage, so I have no interest in extending the behavior to other places in the language. Supporting this feature for compatibility is bad enough.

jwe




reply via email to

[Prev in Thread] Current Thread [Next in Thread]