ERights Home elang / blocks 
Back to: Nested Block Expression On to: The 'while' Expression

The escape Expression


C tradition languages have separate primitive control-flow exiting constructs return , break , and continue . Each of these exit a control-flow sequence, and return also optionally delivers a value. E instead provides the escape expression for defining such control-flow exiting constructs. The hatch-param is bound to an escape-hatch -- an exiting function. If the escape-hatch function is called within body-expression, body expression is exited, and the argument passed to the escape-hatch is the value of the escape expression. If the escape-hatch is not called, then the body expression completes normally, and its value is the value of the escape expression.

For example, here's a useful function that would have been harder to write without escape. Note that "ejector" is not a special name in E. ***say something about how this is a function definition, to be discussed later, but for the moment, works in pretty much the obvious fashion.

? pragma.syntax("0.8")

? def anyOf(list, pred) :any {
>     escape ejector {
>         for item in list {
>             if (pred(item)) {
>                 ejector(true)
>             }
>         }
>         false
>     }
> }
# value: <anyOf>

Given a collection and a predicate, "anyOf" will say whether the predicate is true for any element of the collection. A predicate is merely a function of one argument that returns true or false, such as the following test for even numbers:

? def even(num) :any {
>     num %% 2 == 0
> }
# value: <even>

? anyOf([1,2,3], even)
# value: true

? anyOf([1,3,5], even)
# value: false

Calling the escape-hatch with no arguments is equivalent to calling it with null. For example, a "return()" above would have been equivalent to "return(null)".

 
Unless stated otherwise, all text on this page which is either unattributed or by Mark S. Miller is hereby placed in the public domain.
ERights Home elang / blocks 
Back to: Nested Block Expression On to: The 'while' Expression
Download    FAQ    API    Mail Archive    Donate

report bug (including invalid html)

Golden Key Campaign Blue Ribbon Campaign