control structures (was "Re: Sins")

Evan Simpson evan at tokenexchange.com
Wed Jan 5 11:59:29 EST 2000


Steve Holden <sholden at bellatlantic.net> wrote in message
news:387358BB.E71EC4E7 at bellatlantic.net...
> Phil Jensen wrote:
> >     loop until "element found" or "search failed":
> >         ...
> >         if blah1 == blah2:
> >             "element found"
> >         ...
> >     when "element found":
> >         ...
> >     when "search failed":
> >         ...

I proposed a spelling for this a while back, and have even made sporadic
forays into the Python source to see if I can implement it myself.  It
extends 'try', and would look like this:

try:
    ... nested loops & stuff ...
           break "element found", elem
    ... more stuff ...
        break "search failed"
continue "element found", e:
    print "Found", e
continue "search failed"
    print "Not Found"

> Typically one would use enumerations rather that strings to
> represent the loop states, to combine readability with efficiency.

In this syntax, the strings are treated as labels (despite being string
literals grammatically) and are purely static, for both efficiency and
error-checking.  That is, "break STRING, exprlist" is a compile-time
SyntaxError unless the string exactly matches the string in a "continue"
clause of an enclosing "try" and the expression list is
assignment-compatible with the argument list of that same clause.  Once this
matching is done, the string is discarded and the "break" converted into an
expression evaluation followed by a Jump.  "continue" clauses compile into a
Jump to the end of the "try" followed by an assignment LHS (the target of
the "break" Jump) and the suite body.

In many ways this is quite similar to raising an exception and catching it
in an "except" clause.  The major difference is that everything is totally
resolved at compile time, and is lexically fixed.

> > Whatever the loop is, its exit or exits have some "meaning"
> > in the program, and this control structure encourages an
> > element of Literacy by forcing them to be named.

Exactly.  With no penalty in speed or (compiled) code size, in this case.

Cheers,

Evan @ 4-am





More information about the Python-list mailing list