On 19/09/2014 23:47, Andrea Faulds wrote:
On 19 Sep 2014, at 22:56, Leigh <lei...@gmail.com> wrote:
Loops with a default block, executed in the event that the loop is
never entered.
https://wiki.php.net/rfc/loop_or
While this might be useful, I’d prefer we copy Python’s else behaviour, where a
block of code is executed when break is never used. This feature makes code
that does, for example, a linear search nicer to read.
I saw that discussed in the RFC, and couldn't understand why it would
work that way. I think it's just the word "else" that's throwing me - I
can see that a block distinguishing between "got to the end" and "hit a
break statement" would be useful, but tend to think of "break" as being
the exceptional case, so having that *skip* an "else" block feels really
backwards to me.
It seems like there are actually quite a number of special blocks you
*could* define, such as:
a) When the body is executed zero times (proposed "or" block)
b) When the body is executed exactly once (in a do...while loop, as
mentioned in the RFC)
c) After every iteration of the loop, even if the iteration was ended
early (Perl's "continue" block [1], where "next" is equivalent to PHP's
"continue")
d) After every iteration of the loop, *only* if the iteration was ended
early (would have a similar relationship to (c) as "catch" does to
"finally")
e) After the end of the loop, if "break" was not used (Python's "else"
block [2])
f) After the end of the loop, if "break" WAS used
Most of these can be reduced to conditional jumps: (c) can be
implemented by replacing "continue" with "goto continue_block" [3], and
(d) by skipping that block during normal loop execution [4]; (e) can be
done using a goto which jumps over the relevant block [5], and (f) by
jumping into a skipped block [6]
(a) and (b), on the other hand, I can't think of a way of implementing
in existing PHP code without a state variable to track whether (or how
often) the loop body was reached.
I certainly don't see (a) and (f) as being at all mutually exclusive.
Now I'm going to go to bed before the raptors catch up with me from all
those gotos...
[1] http://perldoc.perl.org/functions/continue.html
[2]
https://docs.python.org/2/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops
[3] http://3v4l.org/TnW12
[4] http://3v4l.org/HbSei
[5] http://3v4l.org/T4Boh
[6] http://3v4l.org/CmO9C
--
Rowan Collins
[IMSoP]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php