Thoughts on PEP315

Stephen Horne $$$$$$$$$$$$$$$$$ at $$$$$$$$$$$$$$$$$$$$.co.uk
Tue Sep 23 20:02:47 EDT 2003


On Tue, 23 Sep 2003 19:26:00 +0100, Tim Rowe
<tim at remove_if_not_spam.digitig.co.uk> wrote:

>On Tue, 23 Sep 2003 17:49:46 +0100, Stephen Horne

>>  while True :
>>    blah; blah; blah; blah
>>    blah; blah; blah; blah
>>  break if condition :
>>    blah; blah; blah; blah
>>    blah; blah; blah; blah
>
>That makes me twitchy: looks like the thin end of a wedge to Perl and
>Ruby's <statement> if <condition> construct.

I don't know Perl that well, and I certainly don't know Ruby. I
certainly wasn't basing this on either of them.

Anyway, you certainly couldn't turn the form above into a <statement>
if <condition> construct.

It is a part of an existing loop construct - *NOT* a statement and
therefore *NOT* a conditional statement. The indentation and the ":"
make it clear that it isn't a statement in its own right. If anything,
it removes the possibility of a future construct like that - consider
the following...


  while True :
    while True :
      inner_loop_stuff ()
    break if ... :
      inner_loop_stuff ()

    break if ...
    #  inner loop ended by 'break if ...' indentation

    outer_loop_stuff ()

If the <statement> if <condition> construct became a reality, both of
those "break if" lines would be legal - but with very different
meanings. The first is a part of the inner loop, and that inner loops
body continues afterwards. The second would be a part of the outer
loop, and its outdent would mark the end the inner loop.

This would be a nightmare for the parser - until it finds the colon or
end-of-line (or semicolon perhaps) it simply wouldn't know which case
it was dealing with.

But if this is bad for the parser, just think what it would do to
users! - forgetting a trailing semicolon shouldn't result in a
statement which is legal yet with a significantly different meaning.
In fact the semicolon on all these things at the moment is IIRC
totally redundant in terms of resolving ambiguity - it is compulsory
for stylistic reasons.

In my mind, "break if" is not the start of a general conditional
statement idea - it is a two-keyword combination in order to avoid
creating a new single keyword such as "until". It may suggest a
"continue if" for reasons of symmetry, but "continue" is IMO useless
anyway so probably not.


-- 
Steve Horne

steve at ninereeds dot fsnet dot co dot uk




More information about the Python-list mailing list