ERights Home elang / blocks 
No Previous Sibling On to: The 'if' Expressions

Control Flow Expressions


Several E design goals are especially clear in the definition of the control-flow expressions -- immediate familiarity + extra power + simplicity . The way E achieves all three is by removing restrictions from the familiar constructs -- restriction we may not have realized were unnecessary.

Immediate familiarity means that if you have already programmed in any of C, C++, Java, Python, Perl, Tcl, or csh (the C-shell), you already know enough of what's in this chapter to get started programming. You may use if , && , || , nested blocks, while , try /catch /finally , and throw the way you are used to. Even without reading this chapter, these should work as you expect. (This is almost true of switch as well, except that E uses a different syntax for the cases.)

Before skipping this chapter, you need one syntactic detail. In order for E to function as a command language, line ends are significant -- they terminate expressions. Wherever curly braces are traditionally expected but optional -- like around the then-part of an if , in E they are required. Curly braces in E must use newlines according to the standard one true brace style -- as shown below:

If this conditional looks familiar to you, you can safely go on to the for loop.

Extra power would seem to imply new features, but too many new features would lose simplicity . Extra power + simplicity would seem to imply replacing the old constructs with simpler or more powerful new ones, but this would loose familiarity. Instead, E redefines the meaning of the familiar constructs in ways that support both existing expectations and new uses, even though the new meaning is usually no more complicated than the old.

Mostly this follows from removing the traditional, but arbitrary, division of code into statements and expressions. In E, everything that might be a statement is instead an expression. For example, E doesn't need a "?:" operator since the if expression will do. Lifting this restriction raises a new question about scoping. The boxes drawn over the control constructs below illustrate E's answer -- a simple change to traditional scoping rules that removes a perpetual minor annoyance.

E does introduce some new constructs (like escape and loop), but we then redefine old constructs (like while) in terms of these. We illustrate all these principles below in our first example -- the if expression.

Control Flow Expressions

if (cond) { then-expr } else { else-expr }
left && right
left || right
{ nested-expr }
escape hatch { body }
while (cond) { body }
try { attempt } catch ex { handler } finally { exit }
switch (specimen) { match pattern { body } ... }
 
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 
No Previous Sibling On to: The 'if' Expressions
Download    FAQ    API    Mail Archive    Donate

report bug (including invalid html)

Golden Key Campaign Blue Ribbon Campaign