[e-lang] Break, Continue, and Return
Mark S. Miller
e-lang@mail.eros-os.org
Sat, 15 Jun 2002 23:54:26 -0700
Another draft note of a resolution for the next release:
Break, Continue, and Return
Prior to this release, the for and while loop shorthands would define
variables named "break" and "continue" as ejectors for exiting the loop, or loop
body respectively. By convention, when E programs needed an ejector for
exiting a method body, they would name the variable holding this ejector
"return". Code explicitly invoking these ejectors would obtain the
conventional effect, but as Ping explains (see example #4 of
http://www.eros-os.org/pipermail/e-lang/2002-June/006704.html ), a
C-syntax-tradition programmer would expect that simply using any of these
names without a following open paren would still cause the corresponding
non-local exit. Of course, when these were just variable names, this did not
occur.
As of this release, these three names are now keywords, to be used as
shorthand for invocations of the corresponding variables __break,
__continue, and __return. For example, the for loop
for i in 1..5 {
println(i)
break
}
now expands to
for i in 1..5 {
println(i)
__break()
}
whose further expansion includes an "escape __break" which defines the
ejector invoked above.
To support conventional syntax for return, any of these ejector keywords can
be invoked with an argument without having to enclose the argument in
parentheses. An example from E's libraries:
/**
* Is the predicate true for any member of the list?
*/
def anyOf(list, pred) :boolean {
escape __return {
for v in list {
if (pred(v)) {
return true
}
}
false
}
}
When exiting a loop with "break expr", the loop as a whole evaluates to the
value of the expression.
Zooko and Dean have each made suggestions that would allow the programmer to
omit the explicit "escape __return", but this release does not yet
incorporate any such proposals. Some of these proposals may likewise require
one to write "return false" rather than "false", but both work in this release.
----------------------------------------
Text by me above is hereby placed in the public domain
Cheers,
--MarkM